通常,如果没有不同的定制购物车项目数据添加到添加到购物车上的购物车项目,WooCommerce会自己执行此操作.
您的代码中存在错误和缺失的步骤.
以下是合并重复产品(购物车项目)的方法:
add_action( 'woocommerce_before_calculate_totals', 'merge_duplicated_products_in_cart');
function merge_duplicated_products_in_cart( $cart ) {
if ((is_admin() && !defined('DOING_AJAX')))
return;
if (did_action('woocommerce_before_calculate_totals') >= 2)
return;
$items_data = $item_update = []; // initializing
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['data']->get_id();
$quantity = $cart_item['quantity'];
// Check if the product exists
if( in_array($product_id, array_keys($items_data)) ) {
// Same product found
$item_update['item_qty'] = $items_data[$product_id]['qty'] + $quantity; // Set cumulated quantities
$item_update['item_key'] = $items_data[$product_id]['key']; // Add first product item key
$item_update['item_remove'] = $cart_item_key; // Add current item key (product to be removed)
break; // Stop the loop
}
// Add product_id, cart item key and item quantity to the array (for each item)
else {
$items_data[$product_id] = array(
'key' => $cart_item_key,
'qty' => $quantity
);
}
}
unset($items_data); // delete the variable
if ( ! empty($item_update) ) {
$cart->remove_cart_item($item_update['item_remove']); // remove last item (same product)
$cart->set_quantity($item_update['item_key'], $item_update['item_qty']); // Update quantity on first item(same product)
unset($item_update); // delete the variable
}
}
代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.
Addition,(based on the OP comment):
以下内容将针对特定定义的产品(S).如果特定产品已经在购物车中,则将数量增加1(define your product(s) ID(s) below):
add_action( 'woocommerce_before_calculate_totals', 'merge_duplicated_products_in_cart');
function merge_duplicated_products_in_cart( $cart ) {
if ((is_admin() && !defined('DOING_AJAX')))
return;
if (did_action('woocommerce_before_calculate_totals') >= 2)
return;
$targeted_product_ids = array(18); // <== HERE set your(s) targeted product(s) ID(s)
$items_data = $item_update = []; // initializing
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['data']->get_id();
// Look for specific products only
if ( in_array($product_id, $targeted_product_ids) ) {
$quantity = $cart_item['quantity'];
// Check if the product is already in cart
if( in_array($product_id, array_keys($items_data)) ) {
// Same product found
$item_update['item_qty'] = $items_data[$product_id]['qty'] + 1; // ADD ONE TO THE QUANTITY
$item_update['item_key'] = $items_data[$product_id]['key']; // Add first product item key
$item_update['item_remove'] = $cart_item_key; // Add current item key (product to be removed)
break; // Stop the loop
}
// Add product_id, cart item key and item quantity to the array (for SPECIFIC items only)
else {
$items_data[$product_id] = array(
'key' => $cart_item_key,
'qty' => $quantity
);
}
}
}
unset($items_data); // delete the variable
if ( ! empty($item_update) ) {
$cart->remove_cart_item($item_update['item_remove']); // remove last item (same product)
$cart->set_quantity($item_update['item_key'], $item_update['item_qty']); // Update quantity on first item(same product)
unset($item_update); // delete the variable
}
}
代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.