I tried the below code but when the cache is disabled. it works, but when I enable cache it does not work.
Overview of retrieving the data of shopping cart items, subtotal, grand total, billing & shipping address in Magento 2
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); echo $totalItems = $cart->getQuote()->getItemsCount(); echo $totalQuantity = $cart->getQuote()->getItemsQty();
I know that we do not use an object manager. I will change the code.