在 woocommerce 3 的购物车页面上显示变体的产品属性
Display product attributes for variations on cart page in woocommerce 3
如何在购物车页面上显示产品类别及其变体?
- 这是关于具有变体的可变产品。
- 变体包含属性。
- 没有附加属性(未针对变体设置)。
要显示变体的产品属性,有两种方法:
查看此相关答案:
要显示产品类别,您可以使用此挂钩函数:
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_data', 10, 3);
function customizing_cart_item_data( $item_name, $cart_item, $cart_item_key ) {
$term_names = array();
// Get product categories
$terms = wp_get_post_terms( $cart_item['product_id'], 'product_cat' );
if( count($terms) > 0 ){
foreach( $terms as $term ) $term_names[] = $term->name;
$item_name .= '<p class="item-category" style="margin:12px 0 0; font-size: .875em;">
<strong class="label">' . _n( 'Category', 'Categories', count($terms), 'woocommerce' ) . ': </strong>
<span class="values">' . implode( ', ', $term_names ) . '</span>
</p>';
}
return $item_name;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
已测试并有效。
如何在购物车页面上显示产品类别及其变体?
- 这是关于具有变体的可变产品。
- 变体包含属性。
- 没有附加属性(未针对变体设置)。
要显示变体的产品属性,有两种方法:
查看此相关答案:
要显示产品类别,您可以使用此挂钩函数:
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_data', 10, 3);
function customizing_cart_item_data( $item_name, $cart_item, $cart_item_key ) {
$term_names = array();
// Get product categories
$terms = wp_get_post_terms( $cart_item['product_id'], 'product_cat' );
if( count($terms) > 0 ){
foreach( $terms as $term ) $term_names[] = $term->name;
$item_name .= '<p class="item-category" style="margin:12px 0 0; font-size: .875em;">
<strong class="label">' . _n( 'Category', 'Categories', count($terms), 'woocommerce' ) . ': </strong>
<span class="values">' . implode( ', ', $term_names ) . '</span>
</p>';
}
return $item_name;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
已测试并有效。