如何在购物车页面上显示产品类别及其变体?
- 这是关于具有变化的可变产品。
- 包含变体的属性。
- 没有附加属性(未针对变体设置)。
要显示变体的产品属性,有两种方法:
请参阅此相关答案:产品变体属性,因为购物车项目在 WooCommerce 中显示不同 https://stackoverflow.com/questions/47871261/product-variations-attributes-as-cart-items-shows-up-differently-in-woocommerce
要显示产品类别,您可以使用此挂钩函数:
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 文件中或任何插件文件中。
经过测试并有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)