Last active
September 5, 2021 00:23
-
-
Save warnakey/62b80036500f6e362858d89907513495 to your computer and use it in GitHub Desktop.
Clear things from the WooCommerce cart the first time you visit a page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This would go in your functions.php file | |
// Clear things out of the cart the first time you visit a specific page (only run this script once) | |
add_action( 'template_redirect', 'clear_cart_items_custom_checkout' ); | |
function clear_cart_items_custom_page() { | |
global $post; | |
$slug = $post->post_name; | |
if($slug == 'exclusive-invitation') { // Replace this with the Slug of the page you want to clear the cart on | |
global $woocommerce; | |
$woocommerce->cart->empty_cart(); | |
} | |
} | |
// Make sure the script to empty things from the custom checkout page cart is only run once | |
add_action( 'init', 'my_run_only_once' ); | |
function my_run_only_once() { | |
if (did_action('init') >= 2 ) | |
return; | |
if(! get_option('run_create_vip_order_once')) { | |
clear_cart_items_custom_page(); // Run the function | |
update_option('run_create_vip_order_once', true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment