1

Observer File :

<?php
  class Gta_MatchProductOrder_Model_Observer
  {
    public function checkproductinorder($Observer)
    {
      $order = $Observer->getEvent()->getOrder();
      $items = $order->getAllItems();
      $skus = array(
                    '654568',
                    '600_1',
                    '757657685',
                    '65786785'); //product SKUs

      $skuList = array();
      foreach ($items as $item)
      {
        if(in_array($item->getSku(), $skus))
        {
          $orderid = $Observer->getEvent()->getOrder()->getIncrementId();
          $skuList[] = $item->getSku();
        }
      }
      Mage::log('The order Id ' . $orderid . ' contains amazon list SKU : '.implode(',',$skuList), null, 'matchorder.log');

      ### start test observer ###
      // $order = $Observer->getEvent();
      // Mage::log($order->getName(),null,'orderproduct.log');
      ### end test observer ###
    }
  }
?>

From above code how to use split or explode function for my sku's. my sku combination : 654568, 757657685, 65786785, 600_1.

fyi = sku 654568, 757657685, 65786785 have 654568_01, 654568_02 & 654568_01, 654568_02 & 65786785_1, 65786785_2 here underscore is unique.

As of now if order contains the product 600_1 return log message, if order contains 654568_01 how can i print in log?

  • this should give you the result your after without exploding if(strpos($item->getSku(), '600_') !== false) { – Dava Gordon Sep 10 '19 at 12:01
1

Yes there is the way, Here you have to just take $item->getSku() in on Variable and then you need to split it, For Ex:

public function checkproductinorder($Observer)

{

$order = $Observer->getEvent()->getOrder();

$items = $order->getAllItems();

foreach ($items as $item) 

{

  $splitSKU = explode('_',$item->getSku()); 

  if($splitSKU[0] == '600')

    {

      Mage::log('yes, the selected product available in order', null, 'matchorder.log');

    }

}

If you use this code spinnet then you can easily get answer.

| improve this answer | |
  • If more than one sku, what i need to do, like, 223344_01, 3456_01, 75657_04, 7456756_06, here underscore unique. – zus Sep 18 '19 at 9:04
  • You need to check first SKU contains _ or not, Then in True 2nd Condition you need to check once string get _ Break that string in 2 parts.Then it works for multiple SKU without any problem – Oscprofessionals Sep 18 '19 at 9:20
  • could you pls explain in brief? – zus Sep 18 '19 at 9:49
  • As my post sku 654568, if order contains 654568_01, i need to trigger? – zus Sep 18 '19 at 9:50
  • One thing, You just change your if condition as if (strpos($splitSKU[0], '_') == false) { – Oscprofessionals Sep 18 '19 at 9:55
1

You can split your sku by following code and get result.

public function checkproductinorder($Observer)
{
    $order = $Observer->getEvent()->getOrder();
    $items = $order->getAllItems();
    foreach ($items as $item) 
    {
        list($underscore_before, $underscore_after) = explode('_', $item->getSku());

        if($underscore_before == '600')
        {
          Mage::log('yes, the slecetd product available in order', null, 'matchorder.log');
        }
    }
}
| improve this answer | |
  • If more than one sku, what i need to do, like, 223344_01, 3456_01, 75657_04, 7456756_06, here underscore unique. – zus Sep 18 '19 at 9:05
  • Hello @Khushbu Mehta, how to print foreach key values in mage::log code : justpaste.it/6nc8d – zus Jan 2 at 5:52
1

instead of if($item->getSku() == '600') you can try

$prefix = '600_';
if (substr($item->getSku(), 0, strlen($prefix)) === $prefix) {
    ...
}
| improve this answer | |
1
public function checkproductinorder($Observer)

 {

  $order = $Observer->getEvent()->getOrder();
  $items = $order->getAllItems();
  foreach ($items as $item) 

  {

    $SKU = explode('_',$item->getSku()); 

    if($SKU[0] == '600'){
      Mage::log('the selected product available in order SKU start with '.$SKU[0], null, 'matchorder.log');
    }


}
| improve this answer | |

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.