1

In Magento 2 how to get current cart details without customer login and get cart details with customer login like minicart ?

I checked with this \Magento\Checkout\Model\Cart & \Magento\Checkout\Model\Session both are not working

I want to get this(cart details) on my custom page(or my custom popup)

I already searched and checked in magento stackexchange,magento community and others blog not get actual results.

I am using this code -

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

// retrieve quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();

// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();

// retrieve 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 />";            
}

That code only working on cart page but not working in other pages. That code is also fine when cache disable but how it will works without cache disable ?

  • Post your code here @sumeetbaja – Prathap Gunasekaran Apr 10 at 10:05
  • @PrathapGunasekaran please check now – sumeet bajaj Apr 10 at 10:13
  • Which type of product you have in cart? – Ashish Viradiya Apr 10 at 10:33
  • @AshishViradiya there only simple products . Above code working only on cart page but not working in other pages. – sumeet bajaj Apr 10 at 10:40
  • Where did you want to place the code? – Arunprabakaran M Apr 10 at 11:17

Your Answer

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

Browse other questions tagged or ask your own question.