Skip to content

Instantly share code, notes, and snippets.

@bernattorras
Created July 17, 2019 12:51
Show Gist options
  • Save bernattorras/d88f947d13b417d04e4713cbbcc77c72 to your computer and use it in GitHub Desktop.
Save bernattorras/d88f947d13b417d04e4713cbbcc77c72 to your computer and use it in GitHub Desktop.
Clears the cart when a specific page is loaded (it can also add a product to the cart automatically)
<?php
add_action( 'template_redirect', 'clear_cart_when_loading_page' );
function clear_cart_when_loading_page() {
$page_id = 124;
if ( is_page( $page_id ) ) {
WC()->cart->empty_cart();
// Uncomment the next lines if you want to add a product to the cart automatically
// $product_id = 1098;
// WC()->cart->add_to_cart( $product_id );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment