获取可变产品的每个 WooCommerce 变体的特定产品属性名称和值

Get specific product attribute name and value for each WooCommerce variation of a variable product

在 WooCommerce 中,我想以我自己的自定义设计显示可变产品作为产品变体的轮播。我得到了可以在数组中显示产品正常价格的代码,但我坚持在其中获取属性名称和值。

基于 "" 答案代码,这是我的实际代码:

 add_action( 'woocommerce_before_single_product_summary', 'custom_table_after_single_product' );

function custom_table_after_single_product(){
    global $product;

   // Only for variable products
   if( ! $product->is_type('variable')) return; 

    $available_variations = $product->get_available_variations();
            // Get product attributes
    $attributes = $product->get_attributes();

    if( count($available_variations) > 0 ){

        $output = ' <section class="boxes">
        <div class="container">
            <div class="row">
            <div class="owl-carousel packed_sections">';

            // Get product attributes


        foreach( $available_variations as $variation ){
            // Get an instance of the WC_Product_Variation object
            $product_variation = wc_get_product($variation['variation_id']);


            $sale_price = $product_variation->get_sale_price();
            if( empty( $sale_price ) ) $sale_price = __( '<em>(empty)</em>', 'woocommerce' );


            $output .= '

                <div class="col-sm-12 package_box">
                    <div class="inside">
                        <div class="number">1000</div>

                        <div class="number_text">Followers</div>
                        <div class="price"><sup>$</sup> <span>'.$product_variation->get_regular_price().'</span></div>
                        <div class="services">
                            <ul>
                                <li><i class="fa fa-video-camera"></i> Include Video Views</li>
                                <li><i class="fa fa-star"></i> High quality likes</li>
                                <li><i class="fa fa-lock"></i> no password required</li>
                                <li><i class="fa fa-clock-o"></i> order start in 60 seconds</li>
                                <li><i class="fa fa-clock-o"></i> fast deliver 10 minutes</li>
                                <li><i class="fa fa-commenting"></i> 24/7 support</li>
                            </ul>
                        </div>
                        <div class="buy"><a href="#">buy now</a></div>
                    </div>
                </div>';


        }
        echo $output;
    }

}

这段代码给出了正确的输出,但我想要属性值和名称,这里也是输出截图:

在下面的代码中,您将能够获取您的特定产品属性名称和术语名称值。您还将获得正确的价格显示(即使产品正在打折)。

您必须在代码中定义您想要的产品属性别名(将 "color" 替换为您的)。

我还在最后添加了 html 缺少的结束标记。

add_action( 'woocommerce_before_single_product_summary', 'custom_table_after_single_product' );
function custom_table_after_single_product(){
    global $product;

    // Only for variable products
    if( ! $product->is_type('variable')) return;

    // HERE below SET your product attribute slug
    $product_attribute_slug = 'color'; // <====  <====  <====  <====  <====  <====  <====

    $targeted_taxonomy      = 'pa_' . $product_attribute_slug;

    $available_variations   = $product->get_available_variations();

    if( count($available_variations) > 0 ){

        $output = ' <section class="boxes">
        <div class="container">
            <div class="row">
            <div class="owl-carousel packed_sections">';

        // Loop through variations
        foreach( $available_variations as $variation_data ){

            // 1. The variation price (handle on sale products clean display)

            if( $variation_data['display_price'] === $variation_data['display_regular_price'] ) {
                $price_to_display = wc_price( $variation_data['display_price'] );
            } else {
                $variation        = wc_get_product($variation_data['variation_id']); // Get the WC_Product_Variation Object
                $price_to_display = wc_get_price_to_display( $variation, array('price' => $variation->get_sale_price() ) );
            }

            // 2. The variation product attribute

            // Loop though product attributes for the current variation
            foreach ( $variation_data['attributes'] as $variation_attribute => $term_slug ) {
                // Get the taxonomy slug
                $taxonomy = str_replace( 'attribute_', '', $variation_attribute );

                // Get the correct product attribute term value for this variation
                if( $taxonomy === $targeted_taxonomy ) {
                    // Get the attribute value term name
                    $term_name = get_term_by( 'slug', $term_slug, $taxonomy )->name;
                }
            }

            $output .= '<div class="col-sm-12 package_box">
                <div class="inside">
                    <div class="number">' . $term_name . '</div>
                    <div class="number_text">' . wc_attribute_label( $targeted_taxonomy ) . '</div>
                    <div class="price"><sup>$</sup> <span>' . strip_tags($price_to_display) . '</span></div>
                    <div class="services">
                        <ul>
                            <li><i class="fa fa-video-camera"></i> ' . __("Include Video Views") . '</li>
                            <li><i class="fa fa-star"></i> ' . __("High quality likes") . '</li>
                            <li><i class="fa fa-lock"></i> ' . __("no password required") . '</li>
                            <li><i class="fa fa-clock-o"></i> ' . __("order start in 60 seconds") . '</li>
                            <li><i class="fa fa-clock-o"></i> ' . __("fast deliver 10 minutes") . '</li>
                            <li><i class="fa fa-commenting"></i> ' . __("24/7 support") . '</li>
                        </ul>
                    </div>
                    <div class="buy"><a href="#">' . __("buy now") . '</a></div>
                </div>
            </div>';
        }

        // Output
        echo $output . '</div>
                </div>
            </div>
        </section>';
    }
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。