magento 2: how to get order details from “controller_action_predispatch_sales_order_reorder” event?

magento.stackexchange https://magento.stackexchange.com/questions/330131

Question

I am using "controller_action_predispatch_sales_order_reorder" event to find out whether an order is reorder or not. Using this event I am able to get original orderId but along with that I want Order details as well which were ordered in the original order.

How can I get that from this event? I have already tried getOrder(), getProduct(), getQoute(), getAllItems() everything is coming up as null.

Here is my code

<?xml version="1.0"?>

<!--    Reorder from Admin side-->
<event name="sales_convert_order_to_quote">
    <observer name="admin_reorder" instance="VoltLighting\OrderInfo\Observer\AdminReorder"/>
</event>

<!--    Reorder from customer side-->
<event name="controller_action_predispatch_sales_order_reorder">
    <observer name="customer_reorder" instance="VoltLighting\OrderInfo\Observer\CustomerReorder"/>
</event>

<event name="checkout_onepage_controller_success_action">
    <observer name="customer_reorder_success" instance="VoltLighting\OrderInfo\Observer\CustomerReorderSuccess"/>
</event>

<event name="checkout_cart_product_add_after">
    <observer name="customer_predispatch_cart" instance="VoltLighting\OrderInfo\Observer\CustomerReorderCart"/>
</event>

Here is my Observer file

<?php

namespace VoltLighting\OrderInfo\Observer;

use Magento\Customer\Model\Session; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface;

class CustomerReorder implements ObserverInterface { protected $customerSession; public function __construct(Session $customerSession) { $this->customerSession = $customerSession; }

public function execute(Observer $observer)
{
    $order = $observer->getEvent();
    $entity_id = $order->getRequest()->getParam('order_id');
    $customer = $this->customerSession;
    $parentOrder = [$entity_id, []];
    $customer->setMyValue($parentOrder);
}

}

Was it helpful?

Solution

In order to get the order details, you need to load the order using the order id, as this event only contains the request which includes the order id, not the order data.

If you need any other clarification, let me know.

UPDATE

protected $orderRepository;

public function __construct(
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
){
$this->orderRepository = $orderRepository;
}

Then you can do following:

$order = $this->orderRepository->get($orderId);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top