0

In response variable, I am getting the last product only; not getting all products but in my log file, I am getting all products.

Here is my code:

class SalesOrderSaveAfter implements ObserverInterface {
    protected $scopeConfig;
    protected $date;
    public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Stdlib\DateTime\DateTime $date)
   { 
      $this->scopeConfig = $scopeConfig;
      $this->date = $date;
      $this->orderData = $orderData;
   }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $enable=$this->scopeConfig->getValue('abc/orderconfig/mkt_order_integration', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        if($enable){ 
            $order = $observer->getEvent()->getOrder();
            $status = 0;
            $name='';
            $data['Email'] = $order->getCustomerEmail();
            $data['FirstName'] = $order->getCustomerFirstname();
            $data['LastName'] = $order->getCustomerLastName();
            $response= $this->orderData->orderData($data,$status,$name);  
            $res= json_decode($response, true); 
            if($res['success']==1){ 
                $productobjectname= $this->scopeConfig->getValue('abc/orderconfig/productcustomobject', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
                $orderobjectname= $this->scopeConfig->getValue('abc/orderconfig/ordercustomobject', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
                $status = 1;   
                foreach($order->getAllVisibleItems() as $item){
                    if($item->getData()){ 
                    $digits = 3;
                    $rndno= rand(pow(10, $digits-1), pow(10, $digits)-1);
                    $cname= $productobjectname;
                    $pdata['emailAddress'] = $order->getCustomerEmail();
                    $pdata['orderId']= $order->getIncrementId();
                    $pdata['incrementId'] = $order->getIncrementId().$item->getProductId();
                    $pdata['productId'] = $item->getProductId();
                    $pdata['productName'] = $item->getName();
                    $pdata['productAmount'] = $item->getPrice();
                    $pdata['productSku'] = $item->getSku();
                    $pdata['productQty'] = (int)$item->getQtyOrdered();
                    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/orderData3.log');
                    $logger = new \Zend\Log\Logger();
                    $logger->addWriter($writer);

                    $logger->info($pdata);  
                    $response = $this->orderData->orderData($pdata,$status,$cname); 
                   }
                }


            }
        }
        return $this;
    }
}

Please check my code and guide me on how can I get all the products to the response variable?

Thanks!

0
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/orderData3.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);  
$i = 0; 
$pdata = array();

foreach($order->getAllVisibleItems() as $item){
     if($item->getData()){ 
         $digits = 3;
         $rndno= rand(pow(10, $digits-1), pow(10, $digits)-1);
         $cname= $productobjectname;
         $pdata[$i]['emailAddress'] = $order->getCustomerEmail();
         $pdata[$i]['orderId']= $order->getIncrementId();
         $pdata[$i]['incrementId'] = $order->getIncrementId();
         $pdata[$i]['productId'] = $item->getProductId();
         $pdata[$i]['productName'] = $item->getName();
         $pdata[$i]['productAmount'] = $item->getPrice();
         $pdata[$i]['productSku'] = $item->getSku();
         $pdata[$i]['productQty'] = (int)$item->getQtyOrdered();      
         $i++;

         $logger->info($pdata);  
       }
    }
$response = $this->orderData->orderData($pdata,$status,$cname); 
  • I am getting all the products in logs but its sending only last product in response – Shomita Dec 23 '19 at 9:37
  • Modified my answer, try this. – Indranil Maity Dec 23 '19 at 9:43
  • what you have changed? – Shomita Dec 23 '19 at 9:44
  • See, I have added $i variable and increase value every time end of the foreach loop and declared an array before the loop. At last set the array value to $response variable. – Indranil Maity Dec 23 '19 at 9:49
  • Moved the $response variable after the foreach loop end. – Indranil Maity Dec 23 '19 at 9:55

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.