cancel
Showing results for 
Search instead for 
Did you mean: 

How can i add if condition for my foreach loop in Observer

SOLVED

How can i add if condition for my foreach loop in Observer

may be stupid question, how can i add if condition for my foreach 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() == 'california')
                {
                    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 shipping state as my filter.

 
1 ACCEPTED SOLUTION

Accepted Solutions

Re: How can i add if condition for my foreach loop in Observer

Hello @Aveeva

First print the $region value, if you getting from $order object, then add simple condition for $items
$region= $order->getRegion();
if($region== 'california')  {
                    foreach($items as $item)
                    {		                $sku = $item->getSku();                        $name= $item->getName();                        $qty = $item->getQtyOrdered();                        $price = $item->getPrice();
                       $loggerInfo = ['sku' => $sku, 'name' => $name, 'qty' => $qty, 'price' => $price];
                        Mage::log(json_encode(print_r($loggerInfo, true)), null, 'event.log', true);
                    }
                }
 

 

 

If my answer is useful, please Accept as Solution & give Kudos

View solution in original post

4 REPLIES 4

Re: How can i add if condition for my foreach loop in Observer

Hello @Aveeva 

 

You can directly get region value from $order object.


$region= $order->getRegion();

OR

  $shipping_state = $Observer->getEvent()->getOrder()->getShippingAddress();
$region_code=$shipping_state->getRegionCode();

 

If my answer is useful, please Accept as Solution & give Kudos

Re: How can i add if condition for my foreach loop in Observer

@Binod Yadav  Yes, how can i use state as a filter using if condition?

Re: How can i add if condition for my foreach loop in Observer

Hello @Aveeva

 

As per your condition in script.

if($shipping_state()->getRegion() == 'california')

What you want to implement further logic with

$items

 if you get the $region value from ShippingAddress

If my answer is useful, please Accept as Solution & give Kudos

Re: How can i add if condition for my foreach loop in Observer

Hello @Aveeva

First print the $region value, if you getting from $order object, then add simple condition for $items
$region= $order->getRegion();
if($region== 'california')  {
                    foreach($items as $item)
                    {		                $sku = $item->getSku();                        $name= $item->getName();                        $qty = $item->getQtyOrdered();                        $price = $item->getPrice();
                       $loggerInfo = ['sku' => $sku, 'name' => $name, 'qty' => $qty, 'price' => $price];
                        Mage::log(json_encode(print_r($loggerInfo, true)), null, 'event.log', true);
                    }
                }
 

 

 

If my answer is useful, please Accept as Solution & give Kudos