I am creating a small ajax add to cart functionality which works but is proving to be relatively expensive from the performance side of things. I am hoping someone can provide a more efficient way to do it.

The problem i think is down to the fact that i need to do things in 4 steps:

  1. Add item or update item
  2. Save cart
  3. Call cart->init()
  4. Save cart

steps 3 and 4 seem redundant but they also seem needed in order to get the new cart contents.

Does anyone know how i can eliminate the last couple of steps but still get the updated cart data after adding or updating?

2 Answers 2


It's kind of difficult to know what exactly you are doing without seeing your code.
But probably, after adding the item to the cart and before you save it, if you call


you should be fine.

To get the list of items in the cart there are several ways to do that. getCart()->getItems() returns a quote item collection, which still contains the deleted items. If you use that method you have to check for those via $item->isDeleted().

Alternatively you can use getCart()->getQuote()->getAllItems() which returns all items except deleted ones, or getCart()->getQuote()->getAllVisibleItems() which returns all items except deleted ones or items that are associated with a configurable or bundled parent item.

  • I am creating my own cart controller to deal with ajax add remove update. I am using getCart()->updateItem() getCart()->removeItem() etc to do these. I am then calling getItems() to get a list of items in the cart and sending that as the response. But the list never changes until the page is manually reloaded Commented Jun 11, 2013 at 19:30
  • The code you posted "setTotalsCollectedFlag" works for updating and adding but not removing. The item still shows in the list after being removed until page loads again manually Commented Jun 11, 2013 at 19:31
  • Updated the answer based on your comment.
    – Vinai
    Commented Jun 12, 2013 at 7:17

I ran up against the same thing not too long ago. Echoing what Vinai stated about not knowing exactly what you're doing without code, I can share with you what solution worked.

First thing, the app/code/core/Mage/Checkout module deleteAction was extended and customized. So, the 'Mage/Checkout/controllers/CartController.php' was included and then the deleteAction contains some logic to see if the request is an AJAX call ( the query parameter 'isAjax' was appended to the delete url ). If the call meets the condition then a try/catch is executed:

try {
                if (!$this->_getCart()->getQuote()->getHasError()){
                    $response['status'] = 'SUCCESS';
                    $response['message'] = $this->__('Item removed');


                    //logic to load mini cart block and response markup
                    //$response['markup'] = $this->getLayout()->getBlock('some.block')->toHtml();
                    Mage::register('referrer_url', $this->_getRefererUrl());

            } catch (Exception $e) {
                $response['status'] = 'ERROR';
                $response['message'] = $this->__('Cannot remove the item.');

I've left out sections of the code intentionally, but I've given you a pretty good start and idea of what i've used.

I think the following portions of the above code will help: $this->_getCart()->removeItem($id)->save(); AND $this->_getSession()->setCartWasUpdated(true);

Hopefully this will do it for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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