
How to get current quote in Magento 2
While working many times we face the requirement to load the current quote of the customer to achieve needed functionality. We can get the current quote of the customer easily by loading the checkout session class \Magento\Checkout\Model\Session. Further we need to call getQuote() method of this class..
Example below will help you to achieve this:
<?php
namespace WebbyTroops\Checkout\Block;
class Quote extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magento\Checkout\Model\Session
*/
protected $_checkoutSession;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Checkout\Model\Session $checkoutSession,
array $data = []
) {
$this->_checkoutSession = $checkoutSession;
parent::__construct($context, $data);
}
/**
* Get current quote of Customer
*/
public function getCurrentQuote()
{
return $this->_checkoutSession->getQuote();
}
}
Get quote data or items/products added in it
Now you have loaded the quote of current customer. Further you can easily get its data like below:
$quote = $block->getCurrentQuote();
echo $quote->getId();
You can also get the items or products added in it using below code snippet:
foreach ($quote->getAllVisibleItems() as $item) {
echo $item->getProductId();
echo $item->getName();
}
Further if you need to load current logged in customer data using customer session then you can follow this article How to get current logged in customer data in Magento 2.
Categories
- Digital Marketing (7)
- eCommerce (34)
- Laravel (1)
- Latest News (5)
- Magento 2 Extensions (5)
- Magento2 (38)
- Shopify (5)
- Shopware (7)
- Wordpress (2)
Recent Posts
Advanced custom links extension (1) auto cancel order (1) banners (1) cancel order (1) cart attributes (1) Customer (7) Custom order invoice number module (1) custom shipping method (1) E-Commerce (7) eCommerce (41) full page cache magento 2 (1) hcaptcha (1) hcaptcha vs recaptcha (1) How to add customer attribute in Magento 2 (1) Laravel 10 (1) Magento2 (34) magento 2 back in stock notification (1) magento 2 cancel order (1) Magento2 Extension (3) magento 2 extensions (1) magento 2 layered navigation (1) magento 2 recaptcha (2) magento 2 robots.txt (1) magento 2.4.6 (1) magento headless commerce (1) new features (1) online store (13) openmaze (1) order management (1) Product (4) product attributes (1) recaptcha (1) search engine optimization (6) seo (10) shipping rule (1) shopify (3) shopware (6) Shopware-plugin (3) Shopware 6 (5) shopware 6 released (1) store switcher (1) structured data (1) upcoming update (1) wordpress (1) wordpress vs shopify (1)