Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get WooCommerce product variation values

I want to be able to list the the variation option values. For example I have a lantern that comes in 12in, 14in and 16in. I want to be able to get these values. I have been trying to use the foreach loop to get these values but I need a little help. here's my code;

function test_func(){
    global $woocommerce, $product, $post;
    // test if product is variable
    if( $product->is_type( 'variable' ) ){
        $available_variations = $product->get_available_variations();
        // var_dump($available_variations);

        foreach( $available_variations as $key => $value ){ 
            var_dump( $value['attributes'] ) ;
        }
    }
}

This is what is output:

array(1) { ["attribute_pa_size"]=> string(4) "12in" } array(1) { ["attribute_pa_size"]=> string(4) "14in" } array(1) { ["attribute_pa_size"]=> string(4) "16in" }

As you can see the values that I want are there, but I don't know how to get them to echo them.

Here's what I get if I var_dump() $available_variations;

" ["backorders_allowed"]=> bool(false) ["dimensions"]=> array(3) { ["length"]=> string(4) "11.8" ["width"]=> string(4) "11.8" ["height"]=> string(4) "11.8" } ["dimensions_html"]=> string(21) "11.8 x 11.8 x 11.8 in" ["display_price"]=> float(3.2) ["display_regular_price"]=> float(3.2) ["image"]=> array(18) { ["title"]=> string(11) "6712R-1.jpg" ["caption"]=> string(0) "" ["url"]=> string(59) "http://website/wp-content/uploads/2018/10/6712R-1.jpg" ["alt"]=> string(0) "" ["src"]=> string(67) "http://website/wp-content/uploads/2018/10/6712R-1-600x600.jpg" ["srcset"]=> string(445) "http://website/wp-content/uploads/2018/10/6712R-1-600x600.jpg 600w, http://website/wp-content/uploads/2018/10/6712R-1-150x150.jpg 150w, http://website/wp-content/uploads/2018/10/6712R-1-300x300.jpg 300w, http://website/wp-content/uploads/2018/10/6712R-1-768x768.jpg 768w, http://website/wp-content/uploads/2018/10/6712R-1-1024x1024.jpg 1024w, http://website/wp-content/uploads/2018/10/6712R-1-100x100.jpg 100w" ["sizes"]=> string(31) "(max-width: 600px) 100vw, 600px" ["full_src"]=> string(59) "http://website/wp-content/uploads/2018/10/6712R-1.jpg" ["full_src_w"]=> int(2000) ["full_src_h"]=> int(2000) ["gallery_thumbnail_src"]=> string(67) "http://website/wp-content/uploads/2018/10/6712R-1-100x100.jpg" ["gallery_thumbnail_src_w"]=> int(100) ["gallery_thumbnail_src_h"]=> int(100) ["thumb_src"]=> string(67) "http://website/wp-content/uploads/2018/10/6712R-1-300x300.jpg" ["thumb_src_w"]=> int(300) ["thumb_src_h"]=> int(300) ["src_w"]=> int(600) ["src_h"]=> int(600) } ["image_id"]=> string(3) "164" ["is_downloadable"]=> bool(false) ["is_in_stock"]=> bool(true) ["is_purchasable"]=> bool(true) ["is_sold_individually"]=> string(2) "no" ["is_virtual"]=> bool(false) ["max_qty"]=> int(17) ["min_qty"]=> int(1) ["price_html"]=> string(145) "

" ["sku"]=> string(5) "6712R" ["variation_description"]=> string(0) "" ["variation_id"]=> int(1462) ["variation_is_active"]=> bool(true) ["variation_is_visible"]=> bool(true) ["weight"]=> string(0) "" ["weight_html"]=> string(3) "N/A" } [1]=> array(24) { ["attributes"]=> array(1) { ["attribute_pa_size"]=> string(4) "14in" } ["availability_html"]=> string(51) "

This is just for one product, there is one for each variation, but this gives you the idea of how it works. I'm also open to trying another method to get the same results so if you know of one let me know. Thanks

like image 318
Reece Avatar asked Oct 31 '18 09:10

Reece


People also ask

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.

How do I get all attributes in WooCommerce?

Access All Product Attribute's And Their Data global $product; // Get all attributes and their data $attributes = $product->get_attributes(); foreach ( $attributes as $attribute ) { if ( is_object($attribute) ) { // Array of attribute data $attribute_data = $attribute->get_data(); // Do what you need to do... } }

How do I get current variation ID in WooCommerce?

To get all variations ID of a variable product, we can use the below code snippet. $product = wc_get_product($product_id); $variations = $product->get_available_variations(); $variations_id = wp_list_pluck( $variations, 'variation_id' ); The above code will provide visible variation IDs only.


1 Answers

You need to use a 2nd foreach loop for the product attributes:

function test_func(){
    global $woocommerce, $product, $post;
    // test if product is variable
    if( $product->is_type( 'variable' ) ){
        // Loop through available product variation data
        foreach ( $product->get_available_variations() as $key => $variation ) {
            // Loop through the product attributes for this variation
            foreach ($variation['attributes'] as $attribute => $term_slug ) {
                // Get the taxonomy slug
                $taxonmomy = str_replace( 'attribute_', '', $attribute );

                // Get the attribute label name
                $attr_label_name = wc_attribute_label( $taxonmomy );

                // Display attribute labe name
                $term_name = get_term_by( 'slug', $term_slug, $taxonmomy )->name;

                // Testing output
                echo '<p>' . $attr_label_name . ': ' . $term_name . '</p>';
            }
        }
    }
}
like image 187
LoicTheAztec Avatar answered Oct 28 '22 01:10

LoicTheAztec