简体   繁体   English

获取WooCommerce中变量产品价格的所有变体属性

[英]Get all variations attributes with prices for a variable product in WooCommerce

There is a need to display Attribute Variations with their respect "Regular Price". 需要以“常规价格”方式显示属性变体。 However despite trying there is no success to display price. 然而,尽管尝试了显示价格没有成功。 Please see the code below that displays the Variation Fine. 请参阅下面显示变化精细的代码。 Please help out to display the Price as well. 请帮忙显示价格。

       //Getting product attributes
    $product_attributes = $product->get_attributes();

    if(!empty($product_attributes)){
        //Getting product attributes slugs
        $product_attribute_slugs = array_keys($product_attributes);
        $count_slug = 0;
        echo '<div class="product_attributes">';
        foreach ($product_attribute_slugs as $product_attribute_slug){
            $count_slug++;

            // Removing "pa_" from attribute slug and adding a cap to first letter
            $attribute_name =  ucfirst( str_replace('pa_', '', $product_attribute_slug) );
            //echo $attribute_name . ' (';

            $attribute_values = get_terms($product_attribute_slug);
            $count_value = 0;
            //print_r(array_values($available_variations));
            foreach($attribute_values as $attribute_value){
                $count_value++;
                $attribute_name_value = $attribute_value->name; // name value
                $attribute_slug_value = $attribute_value->slug; // slug value
                $attribute_slug_value = $attribute_value->term_id; // ID value
                echo $attribute_name_value;
            }
        }
        echo '</div>';
        //print_r(array_values($attribute_values));
    }

You can get all the data that you want for all product variations in a variable product this way: 您可以通过以下方式获取变量产品中所有产品变体所需的所有数据:

if($product->is_type('variable')){
    foreach($product->get_available_variations() as $variation ){
        // Variation ID
        $variation_id = $variation['variation_id'];
        echo '<div class="product-variation variation-id-'.$variation_id.'">
            <strong>Variation id</strong>: '.$variation_id.'<br>';

        // Attributes
        $attributes = array();
        foreach( $variation['attributes'] as $key => $value ){
            $taxonomy = str_replace('attribute_', '', $key );
            $taxonomy_label = get_taxonomy( $taxonomy )->labels->singular_name;
            $term_name = get_term_by( 'slug', $value, $taxonomy )->name;
            $attributes[] = $taxonomy_label.': '.$term_name;
        }
        echo '<span class="variation-attributes">
            <strong>Attributes</strong>: '.implode( ' | ', $attributes ).'</span><br>';

        // Prices
        $active_price = floatval($variation['display_price']); // Active price
        $regular_price = floatval($variation['display_regular_price']); // Regular Price
        if( $active_price != $regular_price ){
            $sale_price = $active_price; // Sale Price
        }
        echo '<span class="variation-prices">
            <strong>Price</strong>: '.$variation['price_html'].'</span><br>
        </div>';
    }
}

This code is tested and works. 此代码经过测试和运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM