• So I noticed that after a successful order, the cart contents were not being cleared.

    I did some debugging and saw that if I manually overwrote the checkout/thankyou.php page, and var_dumped WC()->cart, the cart_contents is empty.

    But if I go to the cart and var_dump the same WC()->cart, the car_contents still contains the previous order details.

    If I call wc_empty_cart on the cart page above (with or without the persistent cart param) then the var_dump(WC()->cart); shows empty contents. As soon as I remove the wc_empty_cart() call and refresh, last order’s products are still in there.

    Basically, unless I am using WC()->cart->empty_cart(); to clear my cart contents manually on the thank you page or via a hook, the carts are not actually cleared and as soon as I leave the page with wc_empty_cart(), the previous cart contents return.

    How would this be possible? Are there multiple versions of the cart instance?

    Additional Info: Nothing is flagged on WC settings page, I genuinely have no hooks which aren’t front end output only, and there is nothing in any error logs (all transactions are working fine with no errors).

    In fact, even If I have not had an order, and hard code wc_empty_cart() onto the top of my cart page, the cart is not emptied – this only happens if I hard code WC()->cart->empty_cart();.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Why not hard code that in?
    The function is this and basically does that for you.

    /**
     * Clears the cart session when called.
     */
    function wc_empty_cart() {
    	if ( ! isset( WC()->cart ) || '' === WC()->cart ) {
    		WC()->cart = new WC_Cart();
    	}
    	WC()->cart->empty_cart( false );
    }
    Thread Starter Jake Whiteley

    (@twentyzerotwo)

    As (badly) explained in OP, when wc_empty_cart() was called during the thankyou.php page by WC, the cart_contents would be restored on the next page view.

    This behavior also happened if I manually triggered wc_empty_cart() on the thank you page myself (on top of the already existing call).

    It was only when I called WC()->cart->empty_cart() directly and on a non thankyou.php template/hook that it was actually cleared.

    It’s behavior which doesn’t make any sense at all, but if I don’t do this manual call then my cart is never emptied after a transaction.

    Sounds strange – is there anything like cacheing?

    Thread Starter Jake Whiteley

    (@twentyzerotwo)

    Not even.

    Ok, I will change tact:

    Is there anything which could go wrong by manually calling a cart refresh on the thankyou page (between the order success condition)?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wc_empty_cart not working as expected’ is closed to new replies.