How to get a current quote ID from a checkout session in Magento 2?

When we are talking about cart processing from the technical side, we often need to get current quotes in Magento 2. It is used for such functions as adding products to the cart, processing them, applying discounts, etc.

Our Request a Quote plugin is a good example of how you can use received code in your custom plugins. This extension allows buyers to request a special quote according to the number of items they want to order. For instance, it may be useful for B2B stores that offer personal discounts for B2C customers. Or, you can allow your B2B customers to get a special B2B quote. In this plugin, we have an additional quote cart into which you can add products to form a request. 

How to get a quote ID from the checkout session Magento 2?

Warning: The code is illustrative. You may need to do some customization to make it work with your Magento 2 installation.

/**
    * @var \Magento\Quote\Model\Quote
    */
    private $quote;
    public function __construct(\Magento\Quote\Model\Quote $quote)
    {
        $this->quote = $quote;
    }
    /**
        * @return int
    */
    public function getQuoteId()
    {
        return $this->quote->getId();
    }

And that’s it. With this code, you can get a cart ID and use it for your purposes in Magento 2.

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.

© 2009-2024 Amasty. All Rights Reserved.