0

How can I add if the condition for my for each loop,

my_observer :

<?php
    class Gta_GetOrderDetails_Model_Observer
    {
        public function getdetailsafterorder($Observer)
        {
            $order = $Observer->getEvent()->getOrder();
            $items = $order->getAllItems();
            $shipping_state = $Observer->getEvent()->getOrder()->getShippingAddress();
            if($shipping_state()->getRegion() == 'Kerala')
                {
                    foreach($items as $item)
                    {
                        $sku = $item->getSku();
                        $name= $item->getName();
                        $qty = $item->getQtyOrdered();
                        $price = $item->getPrice();
                        $shipping_state = $Observer->getEvent()->getOrder()->getShippingAddress();
                        $loggerInfo = ['sku' => $sku, 'name' => $name, 'qty' => $qty, 'price' => $price];
                        Mage::log(json_encode(print_r($loggerInfo, true)), null, 'event.log', true);
                    }
                }
            ### start test observer ###
            // $order = $Observer->getEvent();
            // Mage::log($order->getName(),null,'event.log');
             ### end test observer ###
        } 
    } 
?>

I just want to add shipping state as my filter.

  • what condition you want ? – Shafeel Sha Aug 23 at 8:58
  • If order placed get selected fields like sku,name,qty_ordered,Price and put into my_log.log file in var/log section, here how can i add condition like if order from kerala log not to be create. – zus Aug 23 at 9:02
  • I mean log create only for state Kerala – zus Aug 23 at 9:03
1

Please try this:

foreach($items as $item)
            {
                $sku = $item->getSku();
                $name= $item->getName();
                $qty = $item->getQtyOrdered();
                $price = $item->getPrice();
                $shipping_state = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getRegion()

                if($shipping_state == "Kerala") // only state match 'KERALA' when create log file
                {

                    $loggerInfo = ['sku' => $sku, 'name' => $name, 'qty' => $qty, 'price' => $price];
                    Mage::log(json_encode(print_r($loggerInfo, true)), null, 'event.log', true);
                }

            }
  • Its working, Thank you – zus Aug 23 at 9:23
  • In $loggerInfo how can i add state? i just tried this not working : $loggerInfo = ['sku' => $sku, 'name' => $name, 'qty' => $qty, 'price' => $price, 'shipping_state' => $shipping_state ]; – zus Aug 23 at 9:28
  • please up vote answer it help to find solutions others – Shafeel Sha Aug 23 at 9:29
  • you need to add shipping state in log ? – Shafeel Sha Aug 23 at 9:30
  • yes, i need that also – zus Aug 23 at 9:31

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.