WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Plugin developers: empty_cart bug (1 post)

  1. russellr
    Member
    Posted 3 months ago #

    Hi,

    I'm developing a payment method for WC, and found a bug in WC.

    After returning from the gateway the cart widget was still showing a count of items in the cart, even though I'd cleared the cart.

    Moving to another page shows the correct emptiness of the cart.

    The problem was caused by the session storage in the browser being treated as still correct by cart-fragments.js.

    cart-fragments uses a hash which is stored in a cookie to detect if the contents have changed.

    $woocommerce->cart->empty_cart() does not update the cart cookie!

    That's the bug - I think it should.

    My workaround is to call $woocommerce->cart->get_cart_from_session(); to cause the cookie to update.

    Here's my working "return processing" code:

    if ( $order->status != 'failed' ) {
    	global $woocommerce;
    
    	$woocommerce->cart->empty_cart();
    	//
    	// The following sets the cart cookies afresh and avoids a problem with the cart display not
    	// being updated.  Basically browser session storage
    	// was being shown by the cart-fragments.js script because WC hasn't told it the cart
    	// has been emptied.
    	//
    	$woocommerce->cart->get_cart_from_session();
    }

    A fix to WC would be to add the following line to WC_Cart::empty_cart:
    $this->set_cart_cookies( sizeof( $this->cart_contents ) > 0 );

    https://wordpress.org/plugins/woocommerce/

Reply

You must log in to post.

About this Plugin

About this Topic