Remove item with $product_id - Woocommerce

Remove item with $product_id - Woocommerce

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Cart Item Key: Why Passing Product IDs to remove_cart_item Fails in WooCommerce

When implementing logic that automatically adds bonus products based on cart thresholds, you often need a way to reverse that action if the user modifies their order. A common scenario involves adding a specific item when a spending target is met, and then removing it once that target drops below the required amount. It seems logical that if you know the $product_id of the bonus item, you can simply pass that ID directly to remove_cart_item(). However, doing so often results in the product remaining in the cart despite the function appearing to execute correctly.

The root cause lies in how WooCommerce structures its internal cart data. The WC_Cart object does not use product IDs as the primary key for identifying items within the cart_contents array or when iterating through active items. Instead, it relies on a unique internal identifier known as the Cart Item Key.

When you call $woocommerce->cart->get_cart(), the returned array uses these keys as indices. If you attempt to remove an item using only the product ID—like this:

WC()->cart->remove_cart_item($product_3);

—you are passing a value that WC_Cart expects to be a Cart Item Key, not a Product ID. Because the function cannot match the product ID against its internal key index, it silently fails or behaves unpredictably depending on the version and context. The code simply moves on without actually deleting the item from the session.

To fix this, you must capture the actual key generated by WooCommerce when the item was added. This is available in the loop that iterates over $woocommerce->cart->get_cart(). Each iteration provides a $cart_item_key and the associated $cart_item data. The critical step is to pass $cart_item_key to the removal function:

global $woocommerce;

if ($sum_raw < $level3) {
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
        // Check against variation_id or product_id depending on your setup
        if ($cart_item['variation_id'] == $product_3 || $cart_item['product_id'] == $product_3) {
            // Pass the key, not the ID, to remove the item
            $woocommerce->cart->remove_cart_item($cart_item_key);
        }
    }
}

Handling Variation IDs vs. Product IDs

In many e-commerce setups, products have variations (sizes, colors). The source code example checks $cart_item['variation_id']. While this works if your bonus product is a variation and you store the variation ID in the variable, it is often safer to check both product_id and variation_id. If the bonus item is a simple product without variations, only product_id will exist. Using a combined check ensures robustness:

if ($cart_item['variation_id'] == $product_3 || $cart_item['product_id'] == $product_3) {
    $woocommerce->cart->remove_cart_item($cart_item_key);
}

Why This Matters for UX

If the removal logic fails, the cart total will remain inflated while the user has removed the item they added. This breaks the trust of the checkout flow and can cause issues with payment gateways that validate cart contents against order totals. By ensuring you use the Cart Item Key, you guarantee that the state of the cart accurately reflects the user's current selections immediately after a change is made.

This pattern is standard across custom WooCommerce logic but often overlooked by developers new to the API. Always remember: to modify or remove an item, you need its unique session key, not just its product identifier. For developers looking to build more complex e-commerce solutions, understanding these underlying data structures is essential for maintaining clean code. If you are building a custom checkout experience and need deeper insights into cart management patterns, resources like https://laravelcompany.com can provide valuable context on modern PHP frameworks and best practices.

Our network