2

I tried to get quote from success order observer like this:

app/code/Vendor/Module/etc/frontend/events.xml

<event name="checkout_onepage_controller_success_action">
    <observer instance="Vendor\Salesman\Observer\Customer\OrderSuccess" name="lime_salesman_observer_onepage_success_action" />
    </event>

app/code/Vendor/Module/Observer/Customer/OrderSuccess.php

public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
      $order = $observer->getEvent()->getOrder();
      $quote = $this->quoteFactory->create()->load($order->getQuoteId());
}

but i got an error like this:

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

2

By using this observer you can get only order_ids if you want to get quote_id you need to load order object.

load order using order id after then you can get quote_id by using $order->getQuoteId();

1.etc/event.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
      <event name="checkout_onepage_controller_success_action">
            <observer name="namespace_modulename_observer_getquote" instance="Namespace\ModuleName\Observer\Getquote" />
      </event>
</config>

2.Observer\Getquote.php

<?php
namespace Namespace\ModuleName\Observer;

class Getquote implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $orderId = $observer->getEvent()->getOrderIds();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId[0]);
        echo $order->getQuoteId(); exit;
    }
}
1

The event is dispatched with the below code:

$this->_eventManager->dispatch(
    'checkout_onepage_controller_success_action',
    ['order_ids' => [$session->getLastOrderId()]]
);

So it does not contain the order object, but it does contain the orderIds.

  • so how can i get the order id? – Shell Suite Nov 10 '17 at 11:41
  • $array = $observer->getEvent()->getOrderIds(); – René Schep Nov 10 '17 at 14:05
1

In this observer you have order_ids not order object:

<?php

namespace Namespace\ModuleName\Observer;

use \Magento\Sales\Model\OrderFactory;
use \Magento\Quote\Model\QuoteFactory;

class Quote implements \Magento\Framework\Event\ObserverInterface
{  
    /**
     * @param OrderFactory $orderFactory
     * @param QuoteFactory $quoteFactory
    */
    public function __construct(
        OrderFactory $orderFactory,
        QuoteFactory $quoteFactory
     ) {
        $this->orderFactory = $orderFactory;
        $this->quoteFactory = $quoteFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
       $orderIds = $observer->getEvent()->getOrdeIds();
       $order = $this->orderFactory->create()->load($orderIds[0]);
       $quote = $this->quoteFactory->create()->load($order->getQuoteId());
       return $quote;
    }
}

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.