0

For the reorder function, I added an Observer in my custom module that allows me to extract the quote_id. Actually my real goal is to get the increment id of the order I am reordering for. Currently this is my code but it is not working:

events.xml

<event name="sales_quote_add_item">
        <observer name="custom_reorder_observer" instance="Catalog\Product\Observer\ReorderObserver" />
</event>

ReorderObserver.php

<?php
namespace Catalog\Product\Observer;

use Magento\Framework\Event\ObserverInterface;
 
class ReorderObserver implements ObserverInterface
{
    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
     
    private $orderRepository;

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

    
    //FUNZIONE PER PRELEVARE POSIZIONE SCELTA NEL CALCOLATORE
    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
        
        $quoteItem = $observer->getQuoteItem();
        $quoteId = $observer->getQuoteItem()->getquote_id();
        $orderId = $this->orderRepository->getByQuoteId($quoteId)->getId();
        $quoteItem->setIdreorder($orderId); 
    }
}

How could I do this?

2 Answers 2

1

you can get order id and quote id on reorder like that

<event name="sales_convert_order_to_quote">
    <observer name="giftmessage" instance="Magento\GiftMessage\Observer\SalesEventOrderToQuoteObserver" shared="false" />
</event>

and below

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\GiftMessage\Observer;

use Magento\Framework\Event\ObserverInterface;

/**
 * Gift Message Observer Model
 *
 * @author      Magento Core Team <[email protected]>
 */
class SalesEventOrderToQuoteObserver implements ObserverInterface
{
    /**
     * Gift message message
     *
     * @var \Magento\GiftMessage\Helper\Message|null
     */
    protected $_giftMessageMessage = null;

    /**
     * @var \Magento\GiftMessage\Model\MessageFactory
     */
    protected $_messageFactory;

    /**
     * @param \Magento\GiftMessage\Model\MessageFactory $messageFactory
     * @param \Magento\GiftMessage\Helper\Message $giftMessageMessage
     */
    public function __construct(
        \Magento\GiftMessage\Model\MessageFactory $messageFactory,
        \Magento\GiftMessage\Helper\Message $giftMessageMessage
    ) {
        $this->_messageFactory = $messageFactory;
        $this->_giftMessageMessage = $giftMessageMessage;
    }

    /**
     * Duplicates giftmessage from order to quote on import or reorder
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {


        // Do not import giftmessage data if order is reordered
        if ($order->getReordered()) {
            $order = $observer->getEvent()->getOrder();
            $quote = $observer->getQuote();
        
            $quoteId = $quote['entity_id'];
            //add zend loggers
            $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/reorder.log');
            $logger = new \Zend_Log();
            $logger->addWriter($writer);
            $logger->info('Order ID: ' . $order->getId());        
        }

    }
}
4
  • Are you sure? Because i tried with this code but not work man: $quoteId = $observer->getQuote()->getReservedOrderId();
    – Jackom
    Commented May 21 at 7:58
  • I don't think that is a good idea because sometimes the value of ReservedOrderId is null. I would a function that capture increment order id everytime when i try an reorder. Any suggest?
    – Jackom
    Commented May 21 at 8:14
  • @Jackom code updated that will work, as it is tested.
    – Kareem k
    Commented May 21 at 8:52
  • Sorry man, i tested but for my project this is not good! When i click on button "reorder" this function not write anything about the reorder and i need that write increment id of order inside custom column "idreorder" of table quote_item.
    – Jackom
    Commented May 21 at 9:30
0

you can get the increment ID of the order you are reordering from within the reorder controller.

You can extend the controller and update it as per your requirements.

vendor/magento/module-sales/Model/Reorder/Reorder.php

You can use "$orderNumber" which is the previous order that you are trying to reorder.

2
  • How can i call $orderNumber in my observer? Can you send me an example?
    – Jackom
    Commented May 21 at 13:04
  • As per your code, you are retrieving order from the quote, but the quote doesn't belong to the actual previous order. You want be able to access $orderNumber through the observer. You need to work on controller instead of the observer. Commented May 21 at 13:13

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.