简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM