Magento – Why is checkout_cart_save_after thrown immediately after sales_quote_save_after

checkoutevent-observerquote

The implementation of the Mage_Checkout_Model_Cart::save() method seems to result in duplication of events. Why is the non-standard checkout_cart_save_after event thrown immediately after the generic sales_quote_save_after event would have been thrown by the successful save.

Are there are reasons to listen for one of the events instead of the other? I can see that the sales_quote_save_after event would be thrown by admin or API orders, but they can be excluded from observers via specifying the eventname inside the <frontend> XML node in the module's config.xml anyway.

To provide a more concrete reason for the question, we have inherited code that listens to the checkout_cart_save_after event and I am concerned that it might be missing scenarios when the quote is changed but that event is not thrown.

Reference code from /app/code/core/Mage/Checkout/Model/Cart.php:473:

    Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));

    $this->getQuote()->getBillingAddress();
    $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
    $this->getQuote()->collectTotals();
    $this->getQuote()->save();
    $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId());
    /**
     * Cart save usually called after changes with cart items.
     */
    Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));

Best Answer

If this were a standard ORM model these events would likely exist. Because the Mage_Checkout_Model_Cart model extends Varien_Object instead of Mage_Core_Model_Abstract, these events are dispatched by hand, including the passing of the cart object into the event scope. This allows for similar event-based injection of processing logic related to the cart object before and after the quote save process.