cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot get items from cart on any other page other than cart

Cannot get items from cart on any other page other than cart

I made a custom minicart that is shown as a sidebar. The minicart displays the cart items correctly, but I need to add a cross sell items into the minicart.

 

I modified the minicart code to show 4 items from a special cross sell category, where the client can set which products they want to show in the cross sell section of the minicart. I made the logic like this, if any of the items already in the cart have any cross sell products set, we take those, and fill the rest (up to 4) from the special category. 

 

The problem appears to be getting cart items from the checkout session. Using a custom module helper, block or even plain object manager returns an empty array on any page other that the cart page. 

 

This is the latest code I tried:

<?php
namespace Vendor\CartData\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\View\Element\Template;

class CartItems extends Template implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
    protected $checkoutSession;

    public function __construct(
        Context $context,
        CheckoutSession $checkoutSession,
        array $data = []
    ) {
        $this->checkoutSession = $checkoutSession;
        parent::__construct($context, $data);
    }

    public function getCartItems()
    {
        $quote = $this->checkoutSession->getQuote();
        $items = $quote->getAllItems();

        return $items;
    }

    /**
     * @return array
     */
    public function getArguments()
    {
        return ['cacheable' => false];
    }
}

And I accessed it like this in minicart.phtml: 

$cart_block = $this->getLayout()->createBlock('Vendor\CartData\Block\CartItems');
$cartItems = $cart_block->getCartItems();

If I output count($cartItems), it will output 0 on any page other than cart. The only code that worked was having a file in magento root, which I included like this:

 

// the code to include it in minicart.phtml
include BP . '/cartitems.php';

// the code in the root file
<?php

use Magento\Framework\App\Bootstrap;
use Magento\Checkout\Model\Session as CheckoutSession;

// Include Magento bootstrap file
require __DIR__ . '/app/bootstrap.php';

// Bootstrap Magento
$bootstrap = Bootstrap::create(BP, $_SERVER);

// Object Manager
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend'); // You can use 'adminhtml' for the admin area


// Initialize Checkout Session
$checkoutSession = $objectManager->get(CheckoutSession::class);

// Load the quote
$checkoutSession->getQuote()->load($checkoutSession->getQuote()->getId());

// Get Grand Total
$items = $checkoutSession->getQuote()->getAllItems();

This code works and returns an actual state in the cart, but it also breaks all the add to cart buttons in all the product listings. It also breaks the product page (just a white screen shows).

 

I narrowed the problem down to cache. If I disable caching on the block, it works fine, but as I read, that also disables full page caching on the page that contains a single cacheble="false".

 

I dont know any more things to try. I am using a magento 2.3