VaN
VaN

Reputation: 2210

Cart is empty in custom Observer

I developped a custom Magento module, with an observer :

<?xml version="1.0"?>
    <config>
        <modules>
            <ADF_Evoucher>
                <version>1.0.0</version>
            </ADF_Evoucher>
        </modules>
        <frontend>
            [...]
            <events>
                [...]
                <sales_order_save_before>
                    <observers>
                        <ADF_Evoucher_Model_Observer>
                            <class>evoucher/observer</class>
                            <method>salesOrderSaveBeforeObserve</method>
                        </ADF_Evoucher_Model_Observer>
                    </observers>
                </sales_order_save_before>
            </events>
        </frontend> 

        [...]

    </config>

And my observer :

<?php
class ADF_Evoucher_Model_Observer extends Varien_Event_Observer
{

    public function __construct()
    {
    }

    public function salesOrderSaveBeforeObserve($observer) 
    {

        $order = $observer->getEvent()->getOrder();
        $quote = $order->getQuote();
        $cart = Mage::getModel('checkout/cart');

        Zend_Debug::dump($quote);
        Zend_Debug::dump($cart);
        die();

    }
}
?>

The problem is my cart is empty whereas I did add some items in it :

object(Mage_Checkout_Model_Cart)[458]
  protected '_summaryQty' => null
  protected '_productIds' => null
  protected '_data' => 
    array (size=0)
      empty
  protected '_hasDataChanges' => boolean false
  protected '_origData' => null
  protected '_idFieldName' => null
  protected '_isDeleted' => boolean false
  protected '_oldFieldsMap' => 
    array (size=0)
      empty
  protected '_syncFieldsMap' => 
    array (size=0)
      empty

I can't understand why. Maybe I did something wrong somewhere, I'm kinda new to Magento. Any idea why this cart object is empty in my observer ?

Upvotes: 0

Views: 1385

Answers (2)

Alana Storm
Alana Storm

Reputation: 166046

It's easy to get confused in the cart checkout code — the patterns used here are a little different from the rest of Magento.

The checkout/cart object (Mage_Checkout_Model_Cart) doesn't directly store data. It's more of a service model that's used to group logic related to the checkout, and manage storing certain information in the PHP session.

The items you think of as cart items (a shirt, a pair of shoes, etc.) are actually quote items. You use methods on the cart object to get either

  1. A reference to the quote object

  2. A reference to a collection of quote items

The quote object contains information about the potential order and orderer (customer info, address info, shipping methods, etc. )

The collection of quote items contains information about the items being potentially ordered.

To get the quote from the cart you'd use code like this

$quote = $cart->getQuote();
var_dump(
    $quote->getData()
);

To get the quote items from the cart, you'd use code like this

$items = $cart->getItems();
foreach($items as $item)
{
    var_dump(
        $item->getData()
    );
}

The quote object is (or should be) the same quote reference you're getting from the order.

Hopefully that steers you right. Good luck with your project!

Upvotes: 3

Michael Leiss
Michael Leiss

Reputation: 5660

you are not fetching a singleton, what you do is spawning a new object with

Mage::getModel()

this object is obviously a clean/empty/new object

Upvotes: 1

Related Questions