WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Clearing Woocommerce cart on certain pages (8 posts)

  1. dmitrimerkulov
    Member
    Posted 6 months ago #

    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/

  2. dmitrimerkulov
    Member
    Posted 6 months ago #

    bump

  3. dmitrimerkulov
    Member
    Posted 6 months ago #

    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! :)

  4. dmitrimerkulov
    Member
    Posted 6 months ago #

    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'.

  5. Kloon
    Member
    Posted 6 months ago #

    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');
  6. dmitrimerkulov
    Member
    Posted 6 months ago #

    Thanks, doesn't give any php errors or anything but doesn't empty the cart either :P

  7. dmitrimerkulov
    Member
    Posted 6 months ago #

    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');

  8. dmitrimerkulov
    Member
    Posted 6 months ago #

    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?

Reply

You must log in to post.

About this Plugin

About this Topic