1
votes

In Woocommerce I have these attributes and the terms for them:

color: blue(description=1), red(description=2)
size:  xl(description=10),  m(description=20)

All terms have description fields mentioned above.

For a variable product, I have these variations:

blue-m 
red-xl

For the purpose of auto-generating the variations SKU (based on those descriptions) I need to get the description for the attribute term used in each variant (we use different attributes for each variable product).

For example for the variation with red color and xl size, I want to get something like 210 (from descriptions of red and xl)

I tried using $variation['attributes'] and removing extra characters from it to use with get_terms to get the descriptions of the attributes used in a variant, but I couldn't succeed.

Any Idea?

1

1 Answers

1
votes

This can be done in an easily once you get the Product variation ID $variation_id this way:

// Initializing
$variation_sku = '';

// Get the product variation object (instance)
$variation = wc_get_product( $variation_id );

// Loop through variation attributes "taxonomy" / "terms" pairs
foreach( $variation->get_variation_attributes() as $attribute => $value ){
    $taxonomy = str_replace( 'attribute_', '', $attribute );
    $term = get_term_by( 'slug', $value, $taxonomy );
    // $term_name = $term->name;
    $variation_sku .= $term->description; // <= HERE the Description
}
// Displaying the sku
echo '<p>'.__("SKU: ").$variation_sku.'</p>';