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();
}
}
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();
}
foreach ($allItems as $item) {
echo $item->getProductId();
}