0

I just completed this observer, and i want your opinion if this going to work, the order status will based on the ordered product sku.

What i'm trying to achieve is, if the ordered product has sku ends with "FBA", then assign certain status, if not just go normal.

So i defined the state to be used, and calling the skus from the orders, and checking from that in order to check from it.

But yet it's not working for some reason.

Looking forward to hearing from you.

public function changestatus($event) {

$_order = $event->getOrder();

$STATE = 'amazonfba';

foreach($orders as $order){
    $items = $order->getAllVisibleItems();
    foreach($items as $item){
        $sku = $item->getSku();
    }
}
if(strpos($sku, '_FBA') === true){
    $_order->addStatusHistoryComment('Amazon FBA', $STATE)
                   ->setIsVisibleOnFront(false)
                   ->setIsCustomerNotified(false);
}
    return $this;
}
}

EDIT:

Config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Company_FBA>
            <version>0.0.1</version>
        </Company_FBA>
    </modules>
    <global>
        <models>
            <company_fba>
                <class>Company_FBA_Model</class>
            </company_fba>
        </models>
    </global>
    <adminhtml>
        <events>
            <sales_order_place_after><!-- observe the event -->
                <observers>
                    <company_fba>
                        <type>singleton</type>
                        <class>company_fba/observer</class>
                        <method>changestatus</method>
                    </company_fba>
                </observers>
            </sales_order_place_after>
        </events>
    </adminhtml>
</config>
  • You can explain more about the purpose of this observer? – Khoa TruongDinh Sep 2 '16 at 16:54
  • Well, simply if condition met, assign the orderstatus. – JAMES Sep 2 '16 at 16:55
  • Which event observer you tried to catch? Please add more details. – Khoa TruongDinh Sep 2 '16 at 17:22
0

That if condition only checks if the last item in the last order does not contain _FBA string or the sku starts with _FBA (strpos returns the position of the searched string in haystack, 0 indexed). So you should check for false value like this strpos(...) === false.

I suggest you to concatenate all sku values into a string ( like $sku .= $item->getSku(), initialize $sku variable before first foreach ) or creats an array of sku's and check with in_array function.

As a side note, you should tell us what are you trying to achieve and what observer do you use for that purpose

  • Updated the original post, please read it. – JAMES Sep 3 '16 at 4:39
  • You are placing the event in the adminhtml area, so the observer won't trigger when placing the order in frontend, so place events tag under global tag. $orders variable isn't initialized. Remove the first foreach and pay attention to variable naming. You should use $_order->getAllVisibleItems(). – theodorhanu Sep 3 '16 at 6:23
  • Also, you are looping through all order items but check for the last product's sku. I would suggest you to move if statement inside foreach. The condition should be if( substr($sku,-4) == '_FBA') { ..additional code here; break; } break is used to break out of for since we found a matching product. – theodorhanu Sep 3 '16 at 6:30
  • At last, you should save the $_order variable after you change it's properties: $_order->addStatus.......->save(); – theodorhanu Sep 3 '16 at 6:33
  • public function changestatus($event) { $_order = $event->getOrder(); $STATE = 'amazonfba'; $items = $_order->getAllVisibleItems() foreach($items as $item){ $sku = $item->getSku() } if( substr($sku,-4) == '_FBA'){ $_order->addStatusHistoryComment('Amazon FBA', $STATE) ->setIsVisibleOnFront(false) ->setIsCustomerNotified(false) ->save(); break; } Mage::log('testing'); } } – JAMES Sep 3 '16 at 6:36

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.