0

I've got a basic observer function to grab all of the gift items in an order and return them:

public function invoiceGiftCards($observer){

    $order = $observer->getEvent()->getOrder();

    // get all items
    $gift_cards = $order->getItemsCollection(array('giftcard'));

    Mage::log('getSize(): ' . $gift_cards->getSize());

    // code to auto-create invoice
    ...

}

Tied to this event:

        <sales_model_service_quote_submit_after>
            <observers>
                <company_invoice_gift_cards>
                    <type>singleton</type>
                    <class>Company_Module_Model_Observer</class>
                    <method>invoiceGiftCards</method>
                </company_invoice_gift_cards>
            </observers>
        </sales_model_service_quote_submit_after>

But what I'm finding is, that collection from the function returns empty every time. Even in orders that clearly have gift cards in them:

2011-06-25T01:33:38+00:00 DEBUG (7): getSize(): 0

Even more strange, if I run that same getItemsCollection(array('giftcard')) on an arbitrary order in the system (as part of a controller), it works fine.

It's like things haven't finished saving yet. Is there another observer I should try? (I also tried checkout_submit_all_after for grins, no dice).

There seems to be something with the order of operations when transactions like orders take place. I've ran into a similar problem before: Creating Invoice & Capturing on Shipment You can see in my comments that my solution there is less than ideal, but I don't know what else to do.

If anyone could shed some light on this for me, I would greatly appreciate it. Thank you.

Community
  • 1
  • 1
bahoo
  • 392
  • 2
  • 13

1 Answers1

0

The quote is what comes before the order, I would guess that at the time of a quote-related event the order does not yet exist, or is incomplete. Perhaps a more relevant time to act would be sales_convert_quote_to_order or sales_order_save_after.

clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • Thanks for your replyu. Using `sales_convert_quote_to_order` still garners me zero: `2011-06-27T15:59:12+00:00 DEBUG (7): getSize(): 0`. I'd rather avoid `sales_order_save_after` so that I don't have to test or manage order states any. – bahoo Jun 27 '11 at 16:01
  • There is also `sales_order_place_after` and doubtless many more too. Magento events don't need to be registered to be fired so there is no complete repository to check, finding the suitable event can take a while. – clockworkgeek Jun 28 '11 at 14:56