View Categories

WooCommerce:在购物车中的产品名称下显示类别

< 1 min read

仿牌独立站出海专家


add_filter( 'woocommerce_cart_item_name', 'bbloomer_cart_item_category', 99, 3);
 
function bbloomer_cart_item_category( $name, $cart_item, $cart_item_key ) {
 
$product_item = $cart_item['data'];
 
// make sure to get parent product if variation
if ( $product_item->is_type( 'variation' ) ) {
$product_item = wc_get_product( $product_item->get_parent_id() );
} 
 
$cat_ids = $product_item->get_category_ids();
 
// if product has categories, concatenate cart item name with them
if ( $cat_ids ) $name .= '</br>' . wc_get_product_category_list( $product_item->get_id(), ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', count( $cat_ids ), 'woocommerce' ) . ' ', '</span>' );
 
return $name;
 
}