Hello, is there something wrong in the below function? I am trying to get product variations name to appear and it’s not working. Please help…
private static function getProductVariationName($orderItem) {
$variationName = array();
if ( !empty($orderItem[‘item_meta’][‘_variation_id’]) ) {
foreach ($orderItem[‘item_meta’][‘_variation_id’] as $variation_id) {
if ( !empty($variation_id) ) {
$variation = wc_get_product($variation_id);
$variationName = $variation->get_title();
$attributes = $variation->get_variation_attributes();
if ( !empty($attributes) ) {
$extraAttributes = array();
foreach ($attributes as $key => $value) {
$attribute_key = urldecode(str_replace(‘attribute_’, ”, $key));
if ( taxonomy_exists($attribute_key) ) {
$term = get_term_by( ‘slug’, $value, $attribute_key);
$label= wc_attribute_label($attribute_key, $orderItem);
if ( !is_wp_error($term)
&& is_object($term)
&& $term->name
&& $label ) {
$extraAttributes[] = $label . ‘: ‘ . $term->name;
}
}
}
if ( !empty($extraAttributes) ) {
$variationName .= ‘ – ‘ . implode(‘, ‘, $extraAttributes);
}
}
}
}
}
return empty($variationName) ? $orderItem[‘name’] : $variationName;
}