0

So, found this snippet that clears the basket and it works well when added to functions.php:

function my_empty_cart(){
    global $woocommerce;
    $woocommerce->cart->empty_cart(); 
}
add_action('init', 'my_empty_cart');

How can I modify this and make it empty the cart only when certain pages are loaded? I played around with if ( is_page( 'pageID' ) but couldn't get it working properly!

Charles
  • 50,943
  • 13
  • 104
  • 142

2 Answers2

0

Try this,

global $post;
if($post->ID == 'something'){
add_action('init', 'my_empty_cart');
}


function my_empty_cart(){
    global $woocommerce;
    $woocommerce->cart->empty_cart(); 
}

for more readings.

check this link it have all the page / function codes in woocommerce

Hope its works..

Jobin
  • 8,238
  • 1
  • 33
  • 52
  • Thanks for this! Unfortunately didn't work. Tried `if ( is_page('order-received') ) {` as well, but still nothing. Weird:/ – user3250136 Jan 30 '14 at 13:15
  • is the post Id getting any value ? this may help http://www.skyverge.com/blog/get-woocommerce-page-urls/ – Jobin Jan 30 '14 at 13:31
  • Ok, tried the `woocommerce_get_page_id( ‘thanks’ );` but still not emptying the cart. Here's the whole snippet: `global $post; if(is_page( woocommerce_get_page_id('thanks'))) { add_action('init', 'my_empty_cart'); } function my_empty_cart(){ global $woocommerce; $woocommerce->cart->empty_cart(); } ` Many thanks!:)) – user3250136 Jan 30 '14 at 16:59
0

WordPress conditional didnt worked for me either so i did something that dont relies on WordPress conditionals:

/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;

if ($_SERVER['REQUEST_URI'] === '/') { 
    $woocommerce->cart->empty_cart(); 
 }
}

if u want to add to certain pages u can edit the conditional exp: (didnt try it)

if ($_SERVER['REQUEST_URI'] === get_permalink('post_id'))

if the get_permalink doest work use the exact url exp: 'https://www.domain.name/post-75'

DrMosko
  • 216
  • 1
  • 9