1

I'm trying to get quote but It return me empty(null).

I'm trying to get this is addToCart.phtml file where it returns empty. and i tried at Magento_Checkout where it works pretty great.

Dont know what's wrong below:

<?php 
$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 />";            
}
?>

Any Help would be appreciated.

  • Can you tell me, in which file you tried this code? i mean .php or .phtml – Balwant Singh Dec 28 '18 at 12:18
  • addToCart.phtml IN vendor for now. – anonymous Dec 28 '18 at 12:19
1

Try below code

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

// retrieve quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();
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 />";            
}
?>

I hope it will help...!!!

  • already tried not working. – anonymous Dec 28 '18 at 12:28
  • @Vivek heve you tried checkout session to get cart items? – Balwant Singh Dec 28 '18 at 12:30

Your Answer

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

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