【发布时间】:2021-04-24 07:14:53
【问题描述】:
如何将定义的产品类别中的商品排序到购物车订单的末尾,例如,我希望属于产品类别“瓶子”的所有产品都位于购物车订单的末尾。
我找到了这个按价格排序的代码,但想按照上面的描述进行调整。
add_action( 'woocommerce_cart_loaded_from_session', 'prefix_cart_order_prod_cat' );
function prefix_cart_order_prod_cat() {
$products_in_cart = array();
// Assign each product's price to its cart item key (to be used again later)
foreach ( WC()->cart->cart_contents as $key => $item ) {
$product = wc_get_product( $item['product_id'] );
$products_in_cart[ $key ] = $product->get_price();
}
// SORTING - use one or the other two following lines:
//asort( $products_in_cart ); // sort low to high
arsort( $products_in_cart ); // sort high to low
// Put sorted items back in cart
$cart_contents = array();
foreach ( $products_in_cart as $cart_key => $price ) {
$cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
}
WC()->cart->cart_contents = $cart_contents;
}
【问题讨论】:
标签: php wordpress woocommerce cart taxonomy-terms