Skip to content

Get cart items in Magento 2.4 blocks

I’m trying to get access to all items in the shopping cart in a block, but I can’t seem to get them. If I try the code below to construct and $items = $this->_cart->getQuote()->getAllVisibleItems(); then I get an empty array. Plus this is a deprecated model. Also when I try using MagentoCheckoutModelSession I get an empty array for $items = $this->_session->getQuote()->getAllVisibleItems();.

How are you supposed to get the items in the shopping cart in Magento 2.4?

Older methods don’t seem to work

public function __construct(
    TemplateContext $context,
    MagentoCheckoutModelCart $cart,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->_cart = $cart;
}