I have created a custom block where I am showing the number of items in the cart and also loggedin the customer name. I am getting correct count and customer name on checkout page.But not on any other pages such as Product page, home page etc. This is because I have enabled full page caching. If I disable FPC then everything works fine. But I want to keep FPC and also need to show this count on other pages. How it can be done? I have already tried many codes like $this->_isScopePrivate = true; in constructor. But it's not working. I am using Magento 2.3.1. Below is sample code:
class ShoppingCart extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Checkout\Model\Session $checkoutSession,
array $data = []
) {
$this->_checkoutSession = $checkoutSession;;
parent::__construct($context, $data);
$this->_isScopePrivate = true;
}
/**
* Get quote object associated with cart. By default it is current customer session quote
*
* @return \Magento\Quote\Model\Quote
*/
public function getQuoteData()
{
$this->_checkoutSession->getQuote();
if (!$this->hasData('quote')) {
$this->setData('quote', $this->_checkoutSession->getQuote());
}
return $this->_getData('quote');
}
public function getMyCustomMethod(){
return "test function callecd";
}
}
and calling in logo.phtml page as.
$blockObj= $block->getLayout()->createBlock('Rbj\Customer\Block\ShoppingCart');
$quote = $blockObj->getQuoteData();
$itemsCount = $quote->getItemsCount();
$itemsCount = $blockObj->getMyCustomMethod();
Please advice. Thanks in advance.