TL;DR version
How do I change the name that is automatically generated for variable products?
Detailed version
The product variations all get names based on the variable attributes they generate from.
Example
Let’s say that there’s the varible product: Vase. Vase has the attribute: Condition, with values:
- ‘New’
- ‘Refurbished’
- ‘Used’.
So if I do this (get and output all variation names):
JavaScript
x
$vase = wc_get_product( $vase_product_id );
$variation_ids = $vase->get_children();
if( !empty( $variation_ids ) ):
foreach( $variation_ids as $v_id ):
$variation = wc_get_product( $v_id );
echo '<pre>';
print_r($variation->get_name());
echo '</pre>';
endforeach; // foreach( $variations as $item ):
endif; // if ( !empty( $variations ) )
It will output:
JavaScript
Vase - New
Vase - Refurbished
Vase - Used
How do I change this, so it becomes:
JavaScript
Vase - New condition
Vase - Refurbished condition
Vase - Used condition
… So adding the attribute-name after the variation-value.
Advertisement
Answer
JavaScript
$vase = wc_get_product( $vase_product_id );
$parent_name = $vase->get_name();
$variation_ids = $vase->get_children();
if ( !empty( $variation_ids ) ):
foreach ( $variation_ids as $v_id ):
$variation = wc_get_product( $v_id );
echo '<pre>';
print_r( $parent_name . ' - ' . $variation->get_attribute_summary() );
echo '</pre>';
endforeach; // foreach( $variations as $item ):
endif;