0

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 at 12:01
2

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.

  • If more than one sku, what i need to do, like, 223344_01, 3456_01, 75657_04, 7456756_06, here underscore unique. – zus 12 hours ago
  • 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 12 hours ago
  • could you pls explain in brief? – zus 11 hours ago
  • As my post sku 654568, if order contains 654568_01, i need to trigger? – zus 11 hours ago
  • One thing, You just change your if condition as if (strpos($splitSKU[0], '_') == false) { – oscprofessionals 11 hours ago
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');
        }
    }
}
  • If more than one sku, what i need to do, like, 223344_01, 3456_01, 75657_04, 7456756_06, here underscore unique. – zus 12 hours ago
1

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

$prefix = '600_';
if (substr($item->getSku(), 0, strlen($prefix)) === $prefix) {
    ...
}
  • If matching sku in order i am getting message like : The order Id 1000012 contains amazon list SKU : 654568, if sku not matching in order i am getting empty value message like : The order Id contains amazon list SKU : . Actually i am configured twilio whatsapp messsage service, every time i am getting empty value message also, how to trigger observer only value is matching? @Marius♦ – zus 9 hours ago
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');
    }


}

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.