1

I am trying to get ordered items in sales_order_place_after event, but i am not able to log the ordered items.

Here is the code which i have used.

<?php

namespace Vendor\Module\Helper;

use Magento\Directory\Model\Currency;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Segment;

class Tracking extends AbstractHelper
{
    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @var OrderRepositoryInterface
     */
    private $orderRepository;
    private $segment;
    private $currency;
    private $storeManager;

    public function __construct(
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\View\Page\Title $pageTitle,
        StoreManagerInterface $storeManager,
        Currency $currency,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        OrderRepositoryInterface $orderRepository,
        \Segment $segment
    ) {
        $this->redirect              = $redirect;
        $this->_pageTitle            = $pageTitle;
        $this->_segment              = $segment;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->orderRepository       = $orderRepository;
        $this->storeManager          = $storeManager;
        $this->currency              = $currency;
    }
    /**
     * @param object $order
     * Process order data & track order completed event
     */
    public function trackOrderCompleted($order)
    {
        if (!$order) {
            return;
        }

        $orderItems = array();

        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('increment_id', $order['increment_id'])->create();
        $orderData = [];
        $order     = $this->orderRepository->getList($searchCriteria);
        if ($order->getTotalCount()) {
            $orderData = $order->getItems();
        }
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/orderData.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($orderData);
        print_r($orderData);exit;
    }
}

1 Answer 1

0

$logger->info(json_encode($orderData));

You should encode to JSON the objects or arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.