1 回答
TA贡献1854条经验 获得超8个赞
添加到数组中的产品 ID 将显示在列表底部
function sort_cart_specific_product_at_bottom( $cart ) {
// Product id's to to display at tbe bottom of the product list
$product_ids_last = array( 30, 815 );
// Set empty arrays
$products_in_cart = array();
$products_last = array();
$cart_contents = array();
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id
$product_id = $cart_item['data']->get_id();
// In_array — checks if a value exists in an array
if ( in_array( $product_id, $product_ids_last) ) {
// Add to products last array
$products_last[ $cart_item_key ] = $product_id;
} else {
// Add to products in cart array
$products_in_cart[ $cart_item_key ] = $product_id;
}
}
// Merges the elements together so that the values of one are appended to the end of the previous one.
$products_in_cart = array_merge( $products_in_cart, $products_last );
// Assign sorted items to cart
foreach ( $products_in_cart as $cart_item_key => $product_id ) {
$cart_contents[ $cart_item_key ] = $cart->cart_contents[ $cart_item_key ];
}
// Cart contents
$cart->cart_contents = $cart_contents;
}
add_action( 'woocommerce_cart_loaded_from_session', 'sort_cart_specific_product_at_bottom', 10, 1 );
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报