0

My observer :

   public function checkproductinorder($Observer)
    {
        $order = $Observer->getEvent()->getOrder();
        $items = $order->getAllItems();
        foreach ($items as $item) 
        {
          if($item->getSku() == '600' || '800')
            {
              Mage::log('The selected product list available in order', null, 'matchorder.log');
            }
        }

If the SKU's 600 or 800 order how can I print the SKU value in Mage::Log like,

The selected product list available in order. SKU list : 600, 800

  • 1
    Both of the below solutions working. – zus Aug 30 at 7:02
3

Try below code in your observer, it'll work for you.

public function checkproductinorder($Observer)
{
    $order = $Observer->getEvent()->getOrder();
    $items = $order->getAllItems();
    $skus = array('600','800'); //product SKUs
    $skuList = array();
    foreach ($items as $item) 
    {
      if(in_array($item->getSku(), $skus))
        {
            $skuList[] = $item->getSku();
        }
    }
    if(!empty($skuList)) {
        Mage::log('The selected product list available in order '.implode(',',$skuList), null, 'matchorder.log');
    }
}
  • The above code will add the SKUs list in one line for an order. – Sumit Aug 30 at 6:48
  • Bro you edited your answer after my answer. I answered because your answer will not print sku list in comma separated way in logs otherwise I won't answer it in this question. Anyways +1 for sportsmanship – Muhammad Hasham Aug 30 at 7:04
  • Yes, I've changed my code after your code as it is more optimized then mine. Also, as Zus mentioned, instead "or" operator in_array is better performance and 100's of SKU list is good to use with an array. +1 for your answer for the way it is written. – Sumit Aug 30 at 7:06
  • Every time observer running with empty value if condition not matching-> justpaste.it/2vsv6 how to make observer run only if condition matching? Is this possible? – zus Aug 30 at 10:32
  • I've updated my code, please have a review. – Sumit Aug 30 at 10:34
2

Try this code

public function checkproductinorder($Observer)
{
    $order = $Observer->getEvent()->getOrder();
    $items = $order->getAllItems();
    $skus = array();
    foreach ($items as $item) 
    {
      if($item->getSku() == '600' || $item->getSku() == '800')
        {
         $skus[] = $item->getSku();
        }
    }
    Mage::log('The selected product list available in order. SKU list: '.implode(",",$skus), null, 'matchorder.log');

}

It will print the sku list in comma separated way in log as you mentioned in your question. I hope this will help

  • Your solution is good and working, but instead "or" operator in_array is better performance and 100's of sku list is good to used with array right? – zus Aug 30 at 6:57
  • I think you have only two skus for more than two skus in_array is better choice. He edited his answer after my answer for sku list anyways no worry – Muhammad Hasham Aug 30 at 7:01
  • I really appreciate your mature, thank you for your knowledge sharing. Instead of more number of values your solution is best. – zus Aug 30 at 7:03
  • Every time observer running with empty value if condition not matching-> justpaste.it/2vsv6 how to make observer run only if condition matching? Is this possible? – zus Aug 30 at 10:32

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.