Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
    }
like image 493
maddysanoo Avatar asked Jan 12 '18 17:01

maddysanoo


People also ask

How do I get all product variations in WooCommerce?

In order to get all variations, we use the $product->get_available_variations() method. This code gets all the possible WooCommerce product variations for the current $product.

How do I get attribute value in WooCommerce?

In this article, we'll show you how to get attribute values in WooCommerce. The first thing you need to do is to get the product object. You can do this by using the WC_Product::get_instance() method. Once you have the product object, you can use the WC_Product::get_attribute() method to get an attribute value.


1 Answers

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.

like image 55
LoicTheAztec Avatar answered Oct 06 '22 23:10

LoicTheAztec