WooCommerce: Programmatically Remove Product From Cart Using Product Id.

Code

Want to remove specific WooCommerce product from a cart?

WooCommerce provide ‘WC()->cart->remove_cart_item(string $cart_item_key)’ function to remove a product from cart. if we go through WooCommerce Documentation , wewill find that it accepts cart_item_key as parameter.

So All we need is to get the cart’s item key and remove accordingly.

We Know while adding an item to cart requires just its product ID, removing it from the cart we need to know the “cart item key” .

Many Times in WooCommerce we need to remove a product from cart using product ID, if a certain condition is met we follow these steps:-

  1. Generate Cart Id From Product Id.
  2. Then Find Product In Cart Using Product Cart Id.
  3. If Product Exist Remove Product From cart.

Example :

Add this code to template redirect function.

function ProgrammaticallyRemoveProductFromCart() {
 if ( is_admin() ) return;
 $product_id = 1234;
 $product_cart_id = WC()->cart->generate_cart_id( $product_id );
 $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );
 if ( $cart_item_key ) {
  WC()->cart->remove_cart_item( $cart_item_key );
 }
}
add_action('template_redirect','ProgrammaticallyRemoveProductFromCart'); 

This Function is used when the page reload.
But in some case we need to remove it using the ajax .

No need to worry about it, it’s same process like the previous ,the only difference is previously we used it in template_redirect hook,but now we use it in ajax function.

Add this code for ajax function registration function.

function ProgrammaticallyRemoveProductFromCart() {
 $product_id = $_POST['product_id'];
 $product_cart_id = WC()->cart->generate_cart_id( $product_id );
 $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );
 if ( $cart_item_key ) {
  WC()->cart->remove_cart_item( $cart_item_key );
  return true;
 }
 return false;
}
add_action('wp_ajax_remove_item_from_cart', 'ProgrammaticallyRemoveProductFromCart');
add_action('wp_ajax_nopriv_remove_item_from_cart','ProgrammaticallyRemoveProductFromCart');

Script:
Add this script for calling ajax function.

$.ajax({
 type: "POST",
 url: '#site url#/wp-admin/admin-ajax.php',
 data: {action : 'ProgrammaticallyRemoveProductFromCart','product_id' : '1234'},
 success: function(res) {
  if (res) {
   alert('Removed Successfully');
  }
 }
});

Related reading:

Shiv Kumawat

Executive Director & CEO

Shiv Kumawat is the CEO of Eoxys IT Solution LLP, the Jaipur-based IT and GenAI development company he has led since 2009. Eoxys builds custom AI, web and mobile applications for businesses worldwide. Shiv writes about AI app development, generative AI for business, and building software products that deliver real results. Connect with him on LinkedIn: https://www.linkedin.com/in/shiv-kumawat-eoxys/

Latest Posts

GPT-6.1 Sol vs Claude Sonnet 5.5: isometric illustration of two AI model cubes connected by a data stream on a navy background

GPT-6.1 Sol vs Claude Sonnet 5.5: Which Model for Your App?

GPT-6.1 Sol vs Claude Sonnet 5.5 is the mid-tier choice most founders are making right now. Both models launched in…

Shiv Kumawat
October 9, 2026
Siri AI integration for iOS apps: isometric illustration of an iPhone app connected to a glowing assistant orb and app action tiles

Siri AI Integration for iOS Apps: How to Get Your App Ready

Siri AI started rolling out in mid-September with iOS 27, and it can now find content inside apps and act…

Shiv Kumawat
October 8, 2026
Isometric illustration of an online store connected to three AI shopping agents handling product discovery, chat and checkout

AI Shopping Agents for E-commerce: Get Your Store Ready Now

This is the first holiday season where AI shopping agents are a real sales channel. Shopify’s 2026 holiday research, published…

Shiv Kumawat
October 7, 2026
,

Leave a Reply

Your email address will not be published. Required fields are marked *