1

Is there a more efficient way to load and assign quote item custom options/attributes?

This is code in my Observer:

public function MyFunction(Varien_Event_Observer $observer) {
$QuoteObject = Mage::getSingleton('checkout/session')->getQuote();
        foreach ($QuoteObject->getAllVisibleItems() as $item) {
            $custom_attr1 = $item->getData('custom1');
            $custom_attr2 = $item->getData('custom2');
            $custom_attr3 = $item->getData('custom3');
            $custom_attr4 = $item->getData('custom4');
            $custom_attr5 = $item->getData('custom5');
           //do other stuff here
        }
  //do some other stuff here
}
  • which observer you are using ? – Pawan Apr 21 at 1:27
  • @Pawan Am using the sales_order_place_after event in custom observer. Want to know if there is some way to 'get' all the data using an array. Eg to reduce database requests. Or if my existing method is best method. – Dan Apr 21 at 11:21
0

I am not sure about efficient but you can use more elegant way for above code:

You can use $observer object for get Quote.

public function MyFunction(Varien_Event_Observer $observer) {
$QuoteObject = $observer->getEvent()->getQuote();
        foreach ($QuoteObject->getAllVisibleItems() as $item) {
            $custom_attr1 = $item->getData('custom1');
            $custom_attr2 = $item->getData('custom2');
            $custom_attr3 = $item->getData('custom3');
            $custom_attr4 = $item->getData('custom4');
            $custom_attr5 = $item->getData('custom5');
           //do other stuff here
        }
  //do some other stuff here
}
  • Thanks for the tip. That's a small but welcome improvement. – Dan Apr 21 at 13:27
  • welcome @Dan, I think above is the best option, did you find other ? – Pawan Apr 22 at 3:05
  • Unfortunately using $QuoteObject = $observer->getEvent()->getQuote(); didn't work for me (unknown reasons, no exceptions or log errors, but the order process died using this). So I have stuck with using $QuoteObject = Mage::getSingleton('checkout/session')->getQuote(); which works successfully. But thanks for suggesting. – Dan Apr 22 at 17:39
  • you can use checkout_submit_all_after event. – Pawan Apr 22 at 18:04
  • Ok. Unfortunately I don't think that won't work in my flow. But thanks – Dan Apr 22 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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