To Retrieve the current quote ID from the cart checkout page, You need to use a checkout session to fetch all the information related to quote data.
Use Magento\Checkout\Model\Session to inject the object using the class __construct() method. While you working with the checkout page and need to fetch quote data by current quote id, in this case, this article is useful for you.
The checkout session contains the quote data and using session fetch getQuote(), based on the quote, you can get any quote-related data.
<?php declare(strict_types=1); namespace Jesadiya\QuoteId\Model; use Magento\Checkout\Model\Session as CheckoutSession; class CheckoutQuote { public function __construct( private CheckoutSession $checkoutSession ) { } /** * Checkout quote id * * @return int */ public function getQuoteId(): int { return (int)$this->checkoutSession->getQuote()->getId(); } }
Use Function with the method name,
echo $quoteId = $this->getQuoteId();
You can also Optionally declare the Session class as a proxy in the di.xml file to prevent resource-hungry specific classes and call the Session class as needed in the class.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Jesadiya\QuoteId\Model\CheckoutQuote"> <arguments> <argument name="checkoutSession" xsi:type="object">Magento\Checkout\Model\Session\Proxy</argument> </arguments> </type> </config>
Above Given XML File, Pass type name attribute value as Original Class Name with argument name is the checkoutSession and add suffix as the Proxy for the given class.