Adding a clear cart option to your WooCommerce store can be an effective way to improve the shopping experience, particularly when customers may want to reset their cart and start fresh. WooCommerce doesn’t provide a built-in clear cart button by default, but you can easily set one up using a specific URL link and a bit of code.

Here’s a step-by-step guide on creating and implementing a WooCommerce clear cart URL.

Why Add a Clear Cart Option?

A clear cart option can come in handy for various reasons:

Customers who change their minds about items can quickly start fresh.

It can reduce friction for shoppers who may be overwhelmed by the items in their cart.

It allows for easy management of multiple orders, especially if your store often sees customers adding and removing items.

How to Set Up a WooCommerce Clear Cart URL

WooCommerce provides a way to empty the cart programmatically. To enable a “clear cart” link, you can create a URL that, when visited, clears the cart for the user. This URL typically follows this format:

ruby

Copy code

https://yourwebsite.com/?empty-cart

Replace https://yourwebsite.com/ with the base URL of your store. However, for this link to function as a clear cart URL, you need to add some code to make WooCommerce recognize it.

Adding Custom Code to Your Theme’s functions.php

To make the clear cart URL functional, add a code snippet to your theme’s functions.php file. Here’s the code:

php

Copy code

// WooCommerce Clear Cart URL Functionality

add_action('init', 'custom_clear_cart_url');

function custom_clear_cart_url() {

    if (isset($_GET['empty-cart'])) {

        WC()->cart->empty_cart();

    }

}

Steps to Add the Code

In your WordPress dashboard, navigate to Appearance > Theme Editor.

Open the functions.php file of your active theme.

Copy and paste the above code snippet.

Save the changes.

With this code in place, any customer who visits the clear cart URL (like https://yourwebsite.com/?empty-cart) will have their cart emptied.

Ways to Use the Clear Cart URL

Now that the clear cart URL is set up, you can add it to different areas of your site:

Add a Link to Content: You can add the clear cart link to any page, post, or widget in your WordPress editor.

html

Copy code

<a href="https://yourwebsite.com/?empty-cart">Clear Cart</a>

Menu or Sidebar: Create a custom link in your menu or sidebar to make it easy for users to find and use the clear cart option.

CTA Button: You can also create a button instead of a link if it fits better with your store design.

html

Copy code

<a href="https://yourwebsite.com/?empty-cart">Clear Cart</a>

Additional Customization Options

To further enhance the clear cart URL functionality, you may want to customize the user experience after clearing the cart:

1. Redirect Users to a Specific Page

You can customize the code to redirect users to a specific page after clearing their cart. For example, to redirect users to the shop page after they empty their cart, modify the code as follows:

php

Copy code

add_action('init', 'custom_clear_cart_url');

function custom_clear_cart_url() {

    if (isset($_GET['empty-cart'])) {

        WC()->cart->empty_cart();

        wp_redirect(home_url('/shop'));

        exit;

    }

}

In this example, after the cart is cleared, users are automatically taken to the shop page where they can continue shopping.

2. Display a Custom Notice

When users clear their cart, WooCommerce doesn’t automatically show a confirmation message. To improve user experience, you can add a WooCommerce notice. Use the following code snippet to add a custom message after the cart is emptied:

php

Copy code

add_action('init', 'custom_clear_cart_url');

function custom_clear_cart_url() {

    if (isset($_GET['empty-cart'])) {

        WC()->cart->empty_cart();

        wc_add_notice('Your cart has been cleared.', 'notice');

        wp_redirect(wc_get_cart_url());

        exit;

    }

}

This code snippet adds a WooCommerce notice that says “Your cart has been cleared” and then redirects the user back to the cart page, where they can see the notice and confirm the cart is empty.

Creating a Clear Cart Button on the Cart Page

If you want to add a clear cart button directly on the Cart page, you can achieve this with custom code or a plugin. For example, some plugins like Mini Ajax Cart for WooCommerce or WooCommerce Cart Abandonment Recovery offer additional cart management options.

You can also add this button directly using custom HTML on the Cart page template. For example:

html

Copy code

<a href="https://yourwebsite.com/?empty-cart">Clear Cart</a>

If your theme supports custom CSS, you can also style the button to make it look more integrated with the design.

WooCommerce Plugins for Clear Cart Functionality

Using a plugin may be a preferable option if you’re not comfortable adding custom code or if you want extra functionality. Some popular WooCommerce plugins that offer additional cart customization options include:

WooCommerce Customizer – Provides cart management features and other WooCommerce customizations.

WooCommerce Cart Manager – Allows you to add custom buttons to the Cart page, including clear cart and other customizations.

Benefits of Adding a Clear Cart Option

Adding a clear cart option provides your customers with more control over their shopping experience. Some benefits include:

Convenience: Customers can easily clear out unwanted items and start fresh with a single click.

Reduced Abandonment: Shoppers who may feel overwhelmed by items in their cart can quickly remove them, reducing the risk of cart abandonment.

Easy Campaign Management: If your store frequently runs campaigns where customers add and remove items (e.g., limited-time offers), the clear cart option makes it easier for them to manage multiple items in their cart.

Conclusion

By adding a clear cart URL to your WooCommerce store, you’re providing customers with a simple yet effective way to manage their shopping experience. Whether through a custom link, button, or plugin, the clear cart functionality can greatly improve user experience, especially for customers who want the option to reset their cart. With just a bit of custom code, you can implement this feature and make your store more customer-friendly.