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!
From your WordPress Dashboard, navigate to the Appearance > Customize > Header > Header Main Area section. Toggle off the Enable Cart Icon option.
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).
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.
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 );
The updated version of this would be :
WC()->cart->empty_cart();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With