You have a store front using woocommerce and you would like have your visiters to clear the cart then redirect to the Shop page or Plans page (for changing subscriptions).
You just need to add an action that will clear cart items in template redirect hook.
In the custom function, check the current page slug & then clear the cart as per our condition.
Use the hook below in your theme’s functions.php
or in a custom plugin file.
add_action( 'template_redirect', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $post;
$slug = $post->post_name;
if($slug == 'sample-page') {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
or if you don’t want to use a hard coded the page slug, this is the way I like.
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
if ( isset( $_GET['clear-cart'] ) ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
You need to add an action that will clear cart items in template redirect hook.
In the custom function, check the current page slug & then clear the cart as per our condition.
Use the below code snippet in your theme’s functions.php
or custom plugin file.
add_action( 'template_redirect', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $post;
$slug = $post->post_name;
if($slug == 'sample-page') {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
If you don’t like hard coding the page slug, there is also a better method.
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
if ( isset( $_GET['clear-cart'] ) ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
Add the above code in your theme’s functions.php
file.
Then redirect to a page by adding ?clear-cart
query string to the end of your URL & that will clear all cart items.
Example:
or