0

I would like to get the items that added via admin panel in the order page. I have created the observer to listen to Create New Order button event like this :

public function execute(Observer $observer)
{
    $event = $observer->getEvent();
    /** @var OrderInterface $order */
    $order = $event->getOrder();
    /** @var OrderItemInterface[] $orderItems */
    $orderItems = $order->getItems();

    /** @var Quote $quote */
    $quote = $event->getQuote();
    /** @var  CartInterface[] $quoteItems */
    $quoteItems = $quote->getItems();

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $logger= $objectManager->create('\Psr\Log\LoggerInterface');
    $logger->debug('Fabelio Observer::');
    $logger->debug('Fabelio Observer:: item id = ' . $quoteItems[0]->getId());
    $logger->debug('Fabelio Observer:: order item sku ' . $orderItems[0]->getSku());

    $this->quoteSubmitBeforeProcess->processSales($quoteItems,$orderItems);

    return $this;
}

When I try to create new order in the admin panel I got :

Fatal error: Uncaught Error: Call to a member function getId() on null

Now I know that the $quoteItems = $quote->getItems(); giving me a null value.

So How to get the quote items and order items from admin here ? I successfully getting the data from front-end side but not in the admin side .

3
  • Which event you are using ?
    – Keyur Shah
    Commented Oct 10, 2017 at 6:51
  • sales_model_service_quote_submit_before this event. Commented Oct 10, 2017 at 7:38
  • Could you please try with $quote->getAllItems() and check
    – Keyur Shah
    Commented Oct 10, 2017 at 8:11

1 Answer 1

0

I have solve my own problem here, it looks like the

/** @var Quote $quote */
    $quote = $event->getQuote()

is empty during create new order via admin panel. so getting the order event and then get the sku for each item that ordered from admin is the way to do this. here is the working solution :

  • create the events.xml in the adminhtml :

    <event name="sales_model_service_quote_submit_before">
        <observer name="fab_quote_submit_before_admin" instance="Fabelio\Checkout\Observer\OrderSubmitBeforeObserver" />
    </event>
    
  • and create the observer like this :

    class OrderSubmitBeforeObserver implements ObserverInterface
    {
    /**
     * @var OrderSubmitBeforeProcess
     */
    private $orderSubmitBeforeProcess;
    
    
    public function __construct(
        OrderSubmitBeforeProcess $orderSubmitBeforeProcess
    )
    {
        $this->orderSubmitBeforeProcess= $orderSubmitBeforeProcess;
    }
    
    /**
     * {@inheritdoc}
     */
    public function execute(Observer $observer)
    {
        $event = $observer->getEvent();
        /** @var OrderInterface $order */
        $order = $event->getOrder();
        /** @var OrderItemInterface[] $orderItems */
        $orderItems = $order->getItems();
    
        $this->orderSubmitBeforeProcess->process($orderItems);
    
        return $this;
    }
    }
    

that's it.

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.