1

$itemsVisible and $items return 0, only $itemsCollection not empty, but this doesn't work in foreach , thanks for help

$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(); 

$itemsVisible = $cart->getQuote()->getAllItems();

 // get quote items array
 $items = $cart->getQuote()->getAllVisibleItems();

 foreach($itemsCollection 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 />";
}
3
  • you have two $items variable that conflicts one another
    – fmsthird
    Commented Apr 23, 2019 at 8:45
  • 3
    Possible duplicate of Magento 2 - How to get all items in cart?
    – fmsthird
    Commented Apr 23, 2019 at 8:46
  • ok, i removed $items = $cart->getQuote()->getAllVisibleItems();, and in foreach use this $items = $cart->getQuote()->getAllItems(); but still not working
    – Robinio
    Commented Apr 23, 2019 at 8:50

1 Answer 1

0

In that case, your cache is Enable, so you can't get values in an external file.

Once you disable the cache, it will work fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.