Programmatically Remove Product From Cart Using Product Id

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:-

  • Generate Cart Id From Product Id.
  • Then Find Product In Cart Using Product Cart Id.
  • 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');
  }
 }
});

 

Categories

Related Blogs

Convert PHP Array into JavaScript Array.

To convert both a normal PHP array, a multidimensional PHP array, and a nested PHP array into JavaScript arrays, you can use JSON encoding and decoding as previously explained. JSON is well-suited for handling various data structures, including both simple arrays and complex nested arrays.

Add Custom Product Data Tab

Learn how to enhance your WooCommerce product editor by adding a custom product data tab. Follow a step-by-step guide using the woocommerce_product_data_tabs filter and the woocommerce_product_data_panels action. Create a custom tab with your desired label, and include your own fields or content within the panel. Customize your product data to collect and display additional information, providing a tailored experience for your WooCommerce products.

How To Add Menu In WordPress Admin Panel

How to add menu in WordPress admin panel – In this post we see how we can add custom menu to admin sidebar. Sometimes when we on WordPress and we need to show some features or any information in admin page then we can use this code snippet to create the same.

How to add custom style and script in wordpress

`wp_enqueue_scripts` is a crucial action hook in WordPress that allows you to enqueue or register scripts and stylesheets properly. It is commonly used by theme and plugin developers to load necessary CSS and JavaScript files onto the front-end of a WordPress website.