How to clear a Woocommerce cart
Asked Answered
P

8

30

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!

Petiolate answered 6/1, 2014 at 16:25 Comment(0)
E
46

The updated version of this would be :

WC()->cart->empty_cart();
Escaut answered 5/7, 2018 at 14:14 Comment(0)
O
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

Olnay answered 8/1, 2014 at 11:50 Comment(4)
This worked for me, but I had to remove the page and variable check and just directly run $woocommerce->cart->empty_cart();Bolger
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.Melone
@JoãoTeixeira, How you are adding the clear all button on the cart page?Tungusic
@Tungusic 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(); } Caenogenesis
P
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();
} 
Polarization answered 3/6, 2018 at 14:51 Comment(0)
H
4

You can simply call this WooCommerce core functions:

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

To clear cart session:

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

Thanks

Hambletonian answered 6/11, 2018 at 23:23 Comment(1)
To clear the session as well you can simply add true param to the empty_cart( true ) method.Escaut
B
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 );
}
Beccafico answered 12/5, 2020 at 19:36 Comment(0)
E
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();
  }
}
Evoke answered 5/9, 2017 at 9:50 Comment(0)
D
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(); 
 }
}
Derose answered 5/9, 2017 at 3:26 Comment(0)
P
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.

Psychogenic answered 5/9, 2016 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.