29

I am wondering how you can clear the contents of your cart on page load using woocommerce.

I came accross how to add a clear cart button using by adding this in to functions.php

add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
  global $woocommerce;

    if ( isset( $_GET['empty-cart'] ) ) {
        $woocommerce->cart->empty_cart(); 
    }
}

But I was wondering how I'd go about triggering this on say, page load of the home page (if you could specifiy the exact page that would be great, but even the home page would be useful)

Any ideas? Thanks!

8 Answers 8

45

The updated version of this would be :

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

For triggering only on front page your function needs to look like this:

add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
  global $woocommerce;

    if ( is_front_page() && isset( $_GET['empty-cart'] ) ) { 
        $woocommerce->cart->empty_cart(); 
    }
}

function is_front_page() returns true only on front page of your wordpress site. Also, you might detect any other page with function is_page() where you can pass any page title, ID or slug

4
  • This worked for me, but I had to remove the page and variable check and just directly run $woocommerce->cart->empty_cart();
    – codacopia
    Jun 30, 2017 at 19:24
  • This is the complete answer and should be marked as the correct solution. I'd just replace $woocommerce->cart->empty_cart(); with WC()->cart->empty_cart(); which is the updated version. Feb 26, 2020 at 6:49
  • @JoãoTeixeira, How you are adding the clear all button on the cart page? Feb 2, 2021 at 8:16
  • @user9437856 lang-php add_action('woocommerce_after_cart_table', [$this, 'add_empty_cart_button_to_cart']); public function add_empty_cart_button_to_cart() { ob_start(); ?> <a href="<?php echo wc_get_cart_url(); ?>?cart_action=empty" onclick="return confirm('Are you sure you want to remove all items from your cart?'">Empty Cart/a> <?php echo ob_get_clean(); }
    – Morten Bak
    Jun 25, 2021 at 11:08
7

None of the above codes worked on my Wordpress installation (4.9.6). So, I changed the add_action and removed the variable request and went directly to run.

Now my Woocommerce plugin clears products to cart once user exits checkout page without any duplicate errors. Thank you everyone for your help

add_action( 'woocommerce_add_cart_item_data', 'woocommerce_clear_cart_url' );

function woocommerce_clear_cart_url() {

    global $woocommerce;
    $woocommerce->cart->empty_cart();
} 
0
4

You can simply call this WooCommerce core functions:

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

To clear cart session:

WC()->session->set('cart', array());

Thanks

1
  • 1
    To clear the session as well you can simply add true param to the empty_cart( true ) method.
    – Kuliraj
    Jul 29, 2019 at 14:00
4

None of the codes above works on my website. I test it in the latest WordPress version 5.4.1 and the below function works perfectly!

/**
Clears WC Cart on Page Load
(Only when not on cart/checkout page)
*/

add_action( 'wp_head', 'wc_clear_cart' );
function wc_clear_cart() {
    if ( wc_get_page_id( 'cart' ) == get_the_ID() || wc_get_page_id( 'checkout' ) == get_the_ID() ) {
        return;
    }
    WC()->cart->empty_cart( true );
}
2

Try this. I hope it will help you.

add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
  global $woocommerce;

  if (strpos($_SERVER['REQUEST_URI'], '/checkout')  <  0 ) 
  {
         $woocommerce->cart->empty_cart();
  }
}
1

the above didnt worked for me so i needed something that dont relies on WordPress conditional:

/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;

if ($_SERVER['REQUEST_URI'] === '/') { 
    $woocommerce->cart->empty_cart(); 
 }
}
0

If you need empty cart button on cart page you can use below plugin to clear cart

Plugin Name: Empty Cart Button for WooCommerce Link : https://wordpress.org/plugins/woo-empty-cart-button/

No settings needed just activate plugin.

Not the answer you're looking for? Browse other questions tagged or ask your own question.