0

I am struggling to find which event I should use to get the quote id? I want to save some data in db, want to save customer session id and quote id before placing an order?

Can someone suggest which I should use?

1

Use event sales_order_place_before to get quote id before save order

And your observer code would be like this:

public function execute(\Magento\Framework\Event\Observer $observer)
{
   $QuoteID=$observer->getEvent()->getOrder()->getQuoteId();
}
1

This will return current cart details.

public function __construct(
    ...
    \Magento\Checkout\Model\Cart $cart,
    ...
) {
    $this->cart = $cart;
}

public function getCartDetails()
{
    $cart= $this->cart->getQuote()->getAllVisibleItems();
}

you can make an observer for the same on event checkout_cart_save_before

your event.xml will be

 <?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_cart_save_before">
        <observer name="checkout_cart_before" instance="Namespace\Modulename\Observer\GetQuoteId" />
    </event>
</config>
1
  • You can use sales_model_service_quote_submit_success event
  • you can save a value in the order table also.

    public function __construct(
    \Magento\Checkout\Model\Session $checkoutsession,
    \Magento\Sales\Model\Order $orderFacory
    ){
    $this->orderFacory = $orderFacory;
    $this->_checkoutsession = $checkoutsession;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
    $quote = $observer->getQuote();
    $order = $observer->getOrder();
    
    /* get quote id and order id */
    $quoteId = $observer->getQuote()->getQuoteId();
    $orderId = $observer->getOrder()->getOrderId();
    
    /* you can set something in order table */
    $order->setData('field', $array);
    }
    

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.