0

Is there any way to empty or clear the cart when a user adds a new product?

I'm aware of the clear_cart tag but this doesn't work when using the browser's back button, allowing users to add multiple products (or multiple types of products) to the cart.

I think this is happening because the page does not fully reload when using the back button so the clear_cart tag is not fired.

Expresso Store has a empty_cart parameter for it's product tag that empties the cart when a product is added as opposed to when the page loads.

I was wondering if there was anything similar for Cartthrob? Or a workaround that achieves the same thing?

I've thought about forcing the page to reload when using the back button but I'd prefer to avoid this if possible.

0

On clicking the add to cart button you could make an ajax GET request for a template that only contains the clear_cart tag... Although if it were me I'd probably build a quick extension to do this instead so it's not reliant on JavaScript. Would be very quick and easy using the cartthrob_add_to_cart_start hook: https://www.cartthrob.com/docs/developers/hooks/index.html#cartthrob_add_to_cart_start

0

(I'm with Vector; we make CartThrob)

Your "add to cart" link on product pages doesn't need to be a real "add to cart" link. When someone clicks "add to cart" on a product, they can be sent to a template like /store/add_to_cart/{product_id}. Then in your store/add_to_cart template you'd have something like:

{exp:cartthrob:clear_cart}
{exp:cartthrob:add_to_cart entry_id="{segment_3}" quantity="1" return="cart/index"}

ExpressionEngine should parse the clear_cart first, then the add_to_cart, and then redirect the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.