在 WooCommerce 中更改生成的可变产品的产品名称

时间:2021-02-10 16:22:53

标签: php wordpress woocommerce product

TL;DR 版本

如何更改为可变产品自动生成的名称?


详细版本

产品变体都根据它们生成的变量属性获得名称。

示例

假设有可变产品:花瓶。 花瓶有属性:条件,值:

  • '新'
  • '翻新'
  • '使用过'。

所以如果我这样做(获取并输出所有变体名称):

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

它会输出:

Vase - New
Vase - Refurbished
Vase - Used

我该如何改变它,让它变成:

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

... 所以在variation-value 之后添加attribute-name。

1 个答案:

答案 0 :(得分:2)

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