Attribute values for product variations

user3357332 picture user3357332 · Nov 23, 2014 · Viewed 28k times · Source

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!

Answer

helgatheviking picture helgatheviking · Nov 23, 2014

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 );