magento 2 get current quote

Today we talk about how Magento 2 get current quote. You can get quote data from the checkout session. You can get data to manipulate the data in the cart. So let start with our example.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;


class GetQuote extends Action
{

    protected $checkoutSession;

    public function __construct(
              \Magento\Checkout\Model\Session $checkoutSession    
           )
    {
        $this->checkoutSession = $checkoutSession;
    }

    public function getQuotes()
    {
            return $this->checkoutSession->getQuote();

       
    }
}

Now we call our function and get a quote data.

$allItems = $this->getQuotes()->getAllVisibleItems();
foreach ($allItems as $item) {
    echo $item->getProductId();
}

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply