Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute values for product variations

I'm facing a big problem with product variations and their attributes in woocommerce. I'm trying to display a table with each attribute for each availabe product variation. But Woocommerce saves the attributes in post meta complete in lowercase, replaces slashes and german special characters like ü,ö,ä etc. I get the attributes with $variation->get_variation_attributes(). I've searched the database for the save values you can see for example in the dropdown in the admin panel, but they are saved like this without a link to the variation they are assigned to:

a:5:{s:10:"bestell-nr";a:6:{s:4:"name";s:11:"Bestell-Nr.";s:5:"value";s:9:"1 | 2 | and so on...

How can i get the attributes in their correct format to display?

Thanks for your help!

like image 839
user3357332 Avatar asked Nov 23 '14 14:11

user3357332


4 Answers

Actually product attributes are actually terms in custom taxonomies, so you just need to get the terms in that particular taxonomy. All attribute taxonomies are prefaced with 'pa_'. So a size attribute would be a 'pa_size' taxonomy. And the variation ID is the post ID for a variation.

But depending on how you want to display it, WooCommerce has a built-in function for displaying all a variation's attributes:

The following will display a definition list of all of a variations attributes.

echo wc_get_formatted_variation( $product->get_variation_attributes() );

And passing a second parameter of true will display a flat list:

echo wc_get_formatted_variation( $product->get_variation_attributes(), true );
like image 111
helgatheviking Avatar answered Oct 21 '22 09:10

helgatheviking


This seems to work for me. Hope this helps.

$post = get_post();
$id =  $post->ID;
$product_variations = new WC_Product_Variable( $id );
$product_variations = $product_variations->get_available_variations();
print_r($product_variations);

This can be found in the class-wc-product-variable.php

So basically if you look around on that page you can find a bunch of useful functions.

Here is something i put together.

 $product_children = $product_variations->get_children();

 $child_variations = array();

 foreach ($product_children as $child){

 $child_variations[] = $product_variations->get_available_variation($child);

 }

 print_r($child_variations);
like image 43
Delorme-Melesia Grant Avatar answered Oct 21 '22 09:10

Delorme-Melesia Grant


I only wanted to publish one of the variation attributes rather than all of them; contributing this code in case it's helpful to anyone else.

get_variation_attributes() gets all the attributes

wc_get_formatted_variation() returns a formatted version of the array it's handed

$attributes =  $productVariation->get_variation_attributes() ;
if ( $attributes [ 'attribute_pa_colour' ] ) {
    $colour = [ 'attribute_pa_colour' => $attributes [ 'attribute_pa_colour'] ];
    echo wc_get_formatted_variation ( $colour );
}
like image 28
piersb Avatar answered Oct 21 '22 09:10

piersb


The way I do it is by using "get_post_meta":

echo get_post_meta( $variation_id, 'attribute_name_field', true);

Hopes this helps someone.

like image 25
john Avatar answered Oct 21 '22 09:10

john