Skip to content
Advertisement

Change product name for generated variable products in WooCommerce

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

$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:

Vase - New
Vase - Refurbished
Vase - Used

How do I change this, so it becomes:

Vase - New condition
Vase - Refurbished condition
Vase - Used condition

… So adding the attribute-name after the variation-value.

Advertisement

Answer

$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;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement