From 60a83987018a448c824b5cd716e370a2405f21f1 Mon Sep 17 00:00:00 2001 From: Aadil Ayub Date: Tue, 13 May 2025 17:04:47 +0500 Subject: [PATCH] try to fix duplicated cart items --- functions.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/functions.php b/functions.php index 4857d80..b23a9d6 100644 --- a/functions.php +++ b/functions.php @@ -209,3 +209,21 @@ function bbloomer_order_completed_email_add_cc_bcc($headers, $email_id, $order) } return $headers; } + +/** + * Force empty cart before adding new items + * Prevents users from having multiple products in cart at once + */ +function force_single_item_cart($passed, $product_id, $quantity) { + // If cart is not empty and we're adding a new product + if (!WC()->cart->is_empty()) { + // Show notice to the user + // wc_add_notice(__('Your cart has been emptied to add this new item. Only one product can be purchased at a time.', 'somos-infinitos-theme'), 'notice'); + + // Empty the cart + WC()->cart->empty_cart(); + } + + return $passed; +} +add_filter('woocommerce_add_to_cart_validation', 'force_single_item_cart', 10, 3);