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.
Solved! Go to Solution.
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); } }
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();
@Binod Yadav Yes, how can i use state as a filter using if condition?
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
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); } }