I'm trying to get all cart items in a custom block but unable to get all items, it's throwing an error:

Fatal error: Uncaught Error: Call to a member function getAllItems() on null in

Block Code:


namespace Arunendra\Mixin\Block\Cart;

class Index extends \Magento\Framework\View\Element\Template {

    protected $_session;
    protected $collectionFactory;

    public function __construct(
         \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,
        \Magento\Checkout\Model\Session $session
    ) {
        $this->_session = $session;
        $this->collectionFactory = $collectionFactory;


    public function getProductIds()
         $ids = $this->getData('_cart_product_ids');
        if ($ids === null) {
            $ids = [];
            foreach ($this->getQuote()->getAllItems() as $item) {
                $product = $item->getProduct();
                if ($product) {
                    $ids[] = $product->getId();
            $this->setData('_cart_product_ids', $ids);
        return $ids;
    public function getQuote()
        return $this->_session->getQuote();

  • Did you tried with getAllVisibleItems()? – Rohan Hapani Nov 29 '19 at 14:39
  • @RohanHapani Yes, got same error – Arunendra Nov 29 '19 at 14:40
  • It's magento default issue. – Rohan Hapani Nov 29 '19 at 15:12

I know it's bad solution. But, you need to set cacheable="false" in your block from layout file. Like this below way :

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="RH\Helloworld\Block\Helloworld" cacheable="false" name="formbuilder" template="RH_Helloworld::helloworld.phtml"/>

One more way :

You can set $_isScopePrivate = true If content of your block is user specific.

After that, you don't need to set cacheable="false" in your layout xml file.

I tested after set $this->_isScopePrivate = true; in my block's construct and it's return all quote items proper without cacheable="false".

_isScopePrivate is rendering private (session related) information on a cacheable page

Hope, It will helpful for you.

  • How cross sell (Magento\Checkout\Block\Cart\Crosssell) working with cache enabled ? – Arunendra Nov 29 '19 at 15:26
  • You get perfect output now? using isScopePrivate? @Arunendra – Rohan Hapani Nov 30 '19 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.