Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a Woocommerce cart

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!

like image 998
user1370288 Avatar asked Jan 06 '14 16:01

user1370288


People also ask

How do I remove something from my WordPress cart menu?

From your WordPress Dashboard, navigate to the Appearance > Customize > Header > Header Main Area section. Toggle off the Enable Cart Icon option.

How do I clear my cart on Shopify?

Shopify has the ability to clear carts built in, but most themes don't include a “clear cart” button. So, allowing customers to clear a cart with a single click will require us to add an “empty cart” button to your theme, pointing to mystore.com/cart/clear (where mystore.com becomes your store URL).

Can you see abandoned carts on WooCommerce?

The table list view can be accessed at WooCommerce > Carts. The Table view allows the administrator to filter carts by date last updated, Cart status (Open, Abandoned, Converted) and Customer, including Guests, for guest carts.

How do I add items to my cart in WooCommerce?

Easy peasy. You just need to copy and paste the same WooCommerce add-to-cart function changing the product ID. For example: WC()->cart->add_to_cart( 1 ); WC()->cart->add_to_cart( 3 ); WC()->cart->add_to_cart( 2 ); WC()->cart->add_to_cart( 6 );


2 Answers

The updated version of this would be :

WC()->cart->empty_cart(); 
like image 73
Kuliraj Avatar answered Nov 13 '22 13:11

Kuliraj


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

like image 41
mirus Avatar answered Nov 13 '22 12:11

mirus