Cart

WooCommerce Empty Cart Button

WooCommere empty the whole cart with one button click

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About This Solution

By default, WooCommerce only lets users clear items from their cart one by one. This guide shows you how to create one button shown on the cart page which will empty the entire cart in one click. A lot of guides on this topic are using a hook which displays the button next to the coupon field but many store owners don’t have coupons enabled meaning the button does not display. This guide uses the woocommerce_cart_actions hook to display the Empty Cart button next to the Update Cart button.

/**
 * Snippet Name:	WooCommerce Empty Cart Button
 * Snippet Author:	ecommercehints.com
 */

add_action( 'woocommerce_cart_actions', 'ecommercehints_empty_cart_button' ); // Create the button
function ecommercehints_empty_cart_button() {
	echo '<a href="' . esc_url( add_query_arg( 'empty_cart', 'true' ) ) . '" class="button" title="' . esc_attr( 'Empty Cart', 'woocommerce' ) . '">' . esc_html( 'Empty Cart', 'woocommerce' ) . '</a>';
}

add_action( 'wp_loaded', 'ecommercehints_empty_cart_button_empty_cart_action', 20 ); // EMpty the cart if empty button is clicked
function ecommercehints_empty_cart_button_empty_cart_action() {
	if ( isset( $_GET['empty_cart'] ) && 'true' === esc_html( $_GET['empty_cart'] ) ) {
		WC()->cart->empty_cart();
		$referer  = wp_get_referer() ? esc_url( remove_query_arg( 'empty_cart' ) ) : wc_get_cart_url();
		wp_safe_redirect($referer);
	}
}

Snippet Benefits

  • Reduce the number of actions the user has to take to empty their cart, improving the user experience

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 20% off on your first month with us.

26027
WELCOME OFFER