Vote up!
Vote down!

Ajax submit Update Cart button in Cart Block

I'm trying to add ajax to the dc_cart_block, but am having some issues with the line items not refreshing and the form submitting twice.

The site is here: http://piesd.txcsites.com/order-pies/cowboy

The module code is here: http://pastebin.com/Zwn6Cean

If you add something to the cart and then try to update the quantity of a line item you'll see the issues.

The total updates but the line item total does not update and in the $form_state array the new default quantity doesn't refresh. If the page is refreshed then the cart load properly, but the view is loading the line items from a cache or something instead of rebuilding line items after the form submit has gone through.

Second minor issue: the form is submitting twice (Cart update message appears twice for every ajax call)


Asked by: artis
on January 11, 2013

3 Answers

Vote up!
Vote down!

What you probably want to be doing here is building a menu callback that re-renders the whole cart block at once, not updating the individual line items in the cart. That's WAAAAY harder to do than just re-rendering the block in general.

Answer by: EclipseGc
Posted: Jan 11, 2013


Thanks for replying, EclipseGc! Could you expand on that suggestion a bit. I'm not sure where to take it.

- artis on January 11, 2013

I'm not trying to update the individual line items. The module re-loads the entire cart_block view and displays it, but there seems to be some sort of line item cache that is keeping the old line items when the view is built. I've tried resetting the view with:

$view = views_get_view($view_name, TRUE);

but it still doesn't work.
- artis on January 11, 2013
Vote up!
Vote down!

So I switched to using commerce_cart_form view and turned it into a block instead of using the commerce_cart_block. This seemed to work much better. I still had many issues to resolve but the final code is at pastebin here:

I hope it helps someone else create an ajax DC cart.

Answer by: artis
Posted: Jan 18, 2013
Vote up!
Vote down!

Hi, did you make a module on drupal.org from it, or a sandbox. That way others can add patches etc.. greetings, Martijn

Answer by: Summit
Posted: May 24, 2013