更改 WooCommerce 中生成的变量产品的产品名称
Change product name for generated variable products in WooCommerce
TL;DR 版本
如何更改为可变产品自动生成的名称?
详细版
产品变体的名称均基于它们生成的变量属性。
例子
假设有一个可变产品:花瓶。
花瓶具有属性:条件,值为:
- 'New'
- 'Refurbished'
- 'Used'.
所以如果我这样做(获取并输出所有变体名称):
$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
...所以在变化值之后添加属性名称。
$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;
TL;DR 版本
如何更改为可变产品自动生成的名称?
详细版
产品变体的名称均基于它们生成的变量属性。
例子
假设有一个可变产品:花瓶。 花瓶具有属性:条件,值为:
- 'New'
- 'Refurbished'
- 'Used'.
所以如果我这样做(获取并输出所有变体名称):
$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
...所以在变化值之后添加属性名称。
$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;