For some purpose (something like popup) I'm trying to fetch cart details in minicart.phtml from Magento2\vendor\magento\module-checkout\view\frontend\templates\cart\minicart.phtml.
I directly modified core file just for checking purpose only. The point is cart details (from below code) are displaying only in cart page. It is not displaying in other pages like home page, product, category pages.I'm using default Magento EE 2.2.5 Luma theme. Any suggestion will be appreciated...
Below is my code (It's dirty, just for checking purpose),
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
// get quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();
// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
// get quote items array
$items = $cart->getQuote()->getAllItems();
foreach($items as $item) {
echo 'ID: '.$item->getProductId().'<br />';
echo 'Name: '.$item->getName().'<br />';
echo 'Sku: '.$item->getSku().'<br />';
echo 'Quantity: '.$item->getQty().'<br />';
echo 'Price: '.$item->getPrice().'<br />';
echo "<br />";
}
I tried with block also through the constructor (like proper way) magento2ee\vendor\magento\module-checkout\Block\Onepage.php
But same like working only in cart page. Not in other pages..
Update:
I'm just wondering below piece of code for fetching product details is working but not for cart.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */
$collection = $productCollection->addAttributeToSelect('*')
->load();
foreach ($collection as $product){
echo 'Name = '.$product->getName().'<br>';
break;
}
$objectManager->create
instead of$objectManager->get