Attribute Values ​​for Product Variants

I had a big problem with product variations and their attributes in woocommerce. I am trying to show a table with each attribute for each product variant. But Woocommerce retains attributes in post meta in lower case, replaces slashes and special characters of the German language, such as ü, ö, ä, etc. I get attributes with the variable $ var-> get_variation_attributes (). I searched the database for save values, which you can see, for example, in the drop-down list of the admin panel, but they are saved as follows without a link to the option to which they are assigned:

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 display attributes in their correct format?

Thank you for your help!

+6
source share
5 answers

In fact, product attributes are actually terms in custom taxonomy, so you just need to get the conditions in that particular taxonomy. All attribute taxonomies are preceded by "pa_". Thus, the size attribute will be the pa_size taxonomy. And the change identifier is the message identifier for the variation.

But depending on how you want to display it, WooCommerce has a built-in function to display all change attributes:

The following is a list of definitions for all attribute attributes.

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

And when passing the second parameter true , a flat list will be displayed:

 echo wc_get_formatted_variation( $product->get_variation_attributes(), true ); 
+10
source

I just wanted to publish one of the attributes of variation, and not all of them; contributing this code in case it is useful to anyone else.

get_variation_attributes () gets all the attributes

wc_get_formatted_variation () returns a formatted version of the passed array

 $attributes = $productVariation->get_variation_attributes() ; if ( $attributes [ 'attribute_pa_colour' ] ) { $colour = [ 'attribute_pa_colour' => $attributes [ 'attribute_pa_colour'] ]; echo wc_get_formatted_variation ( $colour ); } 
+3
source

It 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

Thus, if you look at this page, you can find a bunch of useful features.

Here is what 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); 
+3
source

How do I do this using get_post_meta:

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

Hope this helps someone.

+1
source

I used wp_get_post_terms to get the correct dispersion attributes.

  global $product; $variations = $product->get_available_variations(); $var = []; foreach ($variations as $variation) { $var[] = $variation['attributes']; } var_dump($var); //xxx to get attribute values with correct lower-upper-mixed-case foreach ($var as $key => $arr) { foreach ($arr as $orig_code => $lowercase_value) { $terms_arr = wp_get_post_terms( $product->id, str_replace('attribute_','',$orig_code), array( 'fields' => 'names' ) ); foreach ($terms_arr as $term) { if (strtolower($term) == $lowercase_value) { $var[$key][$orig_code] = $term; break; } } } } var_dump($var); 

Results:

Before hard code

 array (size=1) 0 => array (size=2) 'attribute_pa_width' => string 'none' (length=4) 'attribute_pa_code' => string 'valancese' (length=9) 

After hard code:

 array (size=1) 0 => array (size=2) 'attribute_pa_width' => string 'none' (length=4) 'attribute_pa_code' => string 'valancese' (length=9) 
0
source

Source: https://habr.com/ru/post/978572/


All Articles