Search the site:

Home Blog Delete all child products of a WooCommerce grouped product from the Cart page

Delete all child products of a WooCommerce grouped product from the Cart page

This question was asked in one of the Facebook groups: WooCommerce Help & Share. It has a strength of 17,000+ members looking to help & share tips about WooCommerce.

A grouped product allows to club together multiple products. It is as good as selecting each product separately & adding them to cart. With a grouped product, you cannot specify any prices for it. It will simply be the total of the child products it has.

Delete WooCommerce group products on cart page - WooCommerce Grouped Products Setup

The details about the child products of a grouped product is stored in the wp_postmeta table in the ‘_children’ meta key.

Delete WooCommerce group products on cart page - WooCommerce Grouped ProductsAnd when the child products are added to the cart from the grouped product page, the group then ceases to exist on the cart page. That is why it is difficult (not impossible) to have a “Delete all” button for a grouped product combination on the cart page.

Instead, what you could do is add a button “Empty Cart” on the Cart page. It’s a fairly simple process that involves 2 steps:

  1. Adding a button
  2. Calling the WooCommerce empty cart function when button is clicked

1. Adding the “Empty Cart” button

The cart page template is located at: templates/cart/cart.php. It has a few hooks that allow you to display any text or HTML. 2 such hooks are woocommerce_before_cart_contents & woocommerce_after_cart_contents.

You can use them as below:

add_action( 'woocommerce_before_cart_contents', array( $this, 'woo_empty_cart' ) );
add_action( 'woocommerce_after_cart_contents', array( $this, 'woo_empty_cart' ) );

where woo_empty_cart is the function in your class file that will be responsible for emptying the WooCommerce cart. Below is the code for the function:

This is how it appears on the WooCommerce cart page:

Delete WooCommerce group products on cart page - View Cart with Empty Cart button

2. Calling the WooCommerce empty cart function when button is clicked

WooCommerce has a function named empty_cart(). This function is defined in includes/class-wc-cart.php file. The function code is as below:

In your custom code, you simply need to call the above empty_cart() when the “Empty Cart” button is clicked. We can call our function on the init hook & check for the appropriate GET variables.

The WooCommerce empty_cart() deletes session data like cart information, coupon information & tax information among others. If the user is a logged-in user, then the function will also destroy the persistent cart that is stored for that user.

Using this method might not solve your purpose if you want multiple “Delete Group” buttons for different group products that are added to cart, but it will allow your customers to remove all products from the WooCommerce cart with a single click.

Browse more in: WooCommerce, WooCommerce How Tos, WooCommerce Tutorials

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
from purchased

Subscribe Now