WordPress.org

Support

Support » Plugins and Hacks » Clearing Woocommerce cart on certain pages

Clearing Woocommerce cart on certain pages

  • dmitrimerkulov
    Member

    @dmitrimerkulov

    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!

    http://wordpress.org/plugins/woocommerce/

Viewing 7 replies - 1 through 7 (of 7 total)
  • dmitrimerkulov
    Member

    @dmitrimerkulov

    bump

    dmitrimerkulov
    Member

    @dmitrimerkulov

    Tried this:

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

    and also this:

    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();
    }

    But still nothing. Help please! 🙂

    dmitrimerkulov
    Member

    @dmitrimerkulov

    oh, just as additional info – trying it to clear the basket on the thank-you page 😉 so in the first snippet, tried ‘order-received’ and ’14’ (which is the pageID) instead of ‘something’.

    Hi, try the following code

    function my_empty_cart(){
        if ( is_page( woocommerce_get_page_id( 'thanks' ) ) ) {
            global $woocommerce;
            $woocommerce->cart->empty_cart();
        }
    }
    add_action('init', 'my_empty_cart');
    dmitrimerkulov
    Member

    @dmitrimerkulov

    Thanks, doesn’t give any php errors or anything but doesn’t empty the cart either 😛

    dmitrimerkulov
    Member

    @dmitrimerkulov

    Is it possible that I somehow hardcode

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

    And then perhaps call this on the thankyou.php?
    add_action('init', 'my_empty_cart');

    dmitrimerkulov
    Member

    @dmitrimerkulov

    Got it kinda working with this:

    function my_empty_cart(){
        if ( is_page( woocommerce_get_page_id( 'thanks' ) ) ) {
            global $woocommerce;
            $woocommerce->cart->empty_cart();
        }
    }
    add_action('get_header', 'my_empty_cart');

    Only thing is, I sometimes have to refresh the page twice for it work and clear the cart. I wonder why?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Clearing Woocommerce cart on certain pages’ is closed to new replies.