在 WooCommerce 中,我使用一个代码来显示牛排重量选择表单,保存选择数据并在购物车、结帐页面、编辑订单时和电子邮件通知中显示这些数据。
我的代码还结合了一个代码,在将任何产品添加到购物车时自动添加包装。添加包装发生在 SKU 上。
有一次,用户@7uc1f3r 帮助进行了自定义排序,使包装始终位于购物车中产品列表的最底部。
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 );
但不幸的是,排序代码有点过时,因为现在包裹是按SKU添加的,而不是按ID添加的。因此,排序不起作用。
考虑到按 SKU 添加包装,如何更改排序代码?
我很乐意为您提供帮助!
慕村225694