我需要按类别显示购物车中的商品。我的产品被分配到一个品牌,我希望产品以其指定的品牌出现在购物车区域。
我需要这样列出:
Cart
Category1
product name category qty price Total
aaa category1 2 50 100
bbb category1 1 10 80
---------------------------------------------------
Category2
product name category qty price Total
test1 category2 2 40 120
test2 category2 1 10 100
-----------------------------------------------------
我正在使用此代码按分类法对产品进行排序:
add_action( 'woocommerce_cart_loaded_from_session', function() {
global $woocommerce;
$products_in_cart = array();
foreach ( $woocommerce->cart->cart_contents as $key => $item ) {
//$cart_cat = $item['wdm_user_custom_data_value'];
$terms = wp_get_post_terms($item['data']->id, 'product-cat' );
$products_in_cart[ $key ] = $terms[0]->name;
}
natsort( $products_in_cart );
$cart_contents = array();
foreach ( $products_in_cart as $cart_key => $product_title )
{
$cart_contents[ $cart_key ] = $woocommerce->cart->cart_contents[ $cart_key ];
}
$woocommerce->cart->cart_contents = $cart_contents;
}, 100 );
如何根据类别在购物车中展示产品?