How to Remove Product from Cart Programmatically in WooCommerce
In this guide, we’ll walk you through how to remove a product from the cart programmatically in WooCommerce using hooks. This method will allow you to remove specific products from the cart based on certain conditions.
Step 1: Hook into woocommerce_cart_item_removed
First, you need to hook into the woocommerce_cart_item_removed
action. This hook is triggered when an item is removed from the cart. You can use this to conditionally remove a specific product.
Add the following code to your theme’s functions.php
file or a custom plugin:
function custom_remove_product_from_cart($cart_item_key, $cart) { // Get the product ID to be removed from the cart $product_id = $cart->cart_contents[$cart_item_key]['product_id']; // Check if you want to remove a specific product based on its ID if ($product_id === YOUR_PRODUCT_ID_TO_REMOVE) { unset($cart->cart_contents[$cart_item_key]); } } add_action('woocommerce_cart_item_removed', 'custom_remove_product_from_cart', 10, 2);
Replace YOUR_PRODUCT_ID_TO_REMOVE
with the actual product ID you want to remove from the cart. You can find the product ID in your WooCommerce product settings.
Step 2: Hook into woocommerce_before_calculate_totals
Next, we will hook into the woocommerce_before_calculate_totals
action. This hook allows you to remove the product from the cart just before the totals are calculated. This ensures that the product is removed properly during the cart calculation process.
Add the following code to your theme’s functions.php
file or a custom plugin:
function custom_remove_product_from_cart_on_calculate_totals($cart) { if (is_admin() && !defined('DOING_AJAX')) { return; } foreach ($cart->get_cart() as $cart_item_key => $cart_item) { // Get the product ID to be removed from the cart $product_id = $cart_item['product_id']; // Check if you want to remove a specific product based on its ID if ($product_id === YOUR_PRODUCT_ID_TO_REMOVE) { $cart->remove_cart_item($cart_item_key); } } } add_action('woocommerce_before_calculate_totals', 'custom_remove_product_from_cart_on_calculate_totals', 10, 1);
Again, replace YOUR_PRODUCT_ID_TO_REMOVE
with the actual product ID you want to remove from the cart.
Step 3: Save Changes and Test the Functionality
Once you’ve added the code to your functions.php
file or custom plugin, save your changes and test the functionality on your WooCommerce store. When the specific product with the provided product ID is added to the cart, it will be removed automatically.
Important: Test the functionality in a staging environment before applying it to your live store to avoid any disruptions. Also, ensure you have a backup of your site before making changes to the code.
Additional Notes
Removing products programmatically from the cart can be useful in various scenarios, such as:
- Product exclusions (e.g., remove specific items based on user roles or cart contents).
- Conditional product removals (e.g., remove an item when a coupon is applied).
Make sure you thoroughly test the changes and ensure that your customers’ shopping experience is not negatively impacted by the removal of products from the cart.