Remove Product From Cart Programmatically WooCommerce

To remove a product from the cart programmatically in WooCommerce, you can use the woocommerce_cart_item_removed and woocommerce_before_calculate_totals hooks. Here’s how you can do it:

Step 1: Hook into woocommerce_cart_item_removed 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.

Step 2: Hook into woocommerce_before_calculate_totals 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 Save the changes to your functions.php file or custom plugin. Now, when you add the specific product with the provided product ID to the cart, it will be removed automatically.

Remember to test the functionality thoroughly to ensure it’s working as expected. Always make sure you have a backup of your site before making any significant changes to the code.

Related Blogs

Add Custom Taxonomy Filter on Products Dashboard.

How To Filter Products by Taxonomies in the Dashboard?

WooCommerce provide Many Product filters on admin screen , such as “Select a category”, “Filter by product type”, “Filter by stock status”.

Delete Product Image With Product Delete In WooCommerce

In WooCommerce, when you delete a product, by default, the product images are not automatically deleted from the server to avoid accidental data loss. However, you can add a custom action to delete the product images when a product is deleted. Here’s a step-by-step guide to achieve this:

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.