1

I'm trying to get all the products of the cart for that I'm trying

use Magento\Checkout\Model\Cart;
$this->_cart = $cart;
public function getCartProducts(){

        $itemsCollection = $this->_cart->getQuote()->getItemsCollection();
    //    echo"<pre>"; print_r($itemsCollection->getData()) ;

        // get array of all items what can be display directly
        $itemsVisible = $this->_cart->getQuote()->getAllVisibleItems();
        // print_r($itemsVisible) ;
        // retrieve quote items array
        $items = $this->_cart->getQuote()->getAllItems();
        print_r($items) ;
        echo"Asad";
        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 />";            
        }

When I run this code then it returns me Null Array in case of $items = $this->_cart->getQuote()->getAllItems(); and $VissibleItems = $this->_cart->getQuote()->getAllVisibleItems(); While in this case $itemsCollection = $this->_cart->getQuote()->getItemsCollection(); it returns me many products which are not even added in cart. So how can I get all the products of the cart??
NOTE: I'm doing this on Localhost (Luma) and I'm doing this for guest user

Edited: I can get the data http://127.0.0.1/magento/checkout/cart/ but on homepage I cannot get the data I want to get the data on homepage because on homepage I get Null Array On homepage it do not return any data enter image description here but on Cart page it shows data enter image description here

1

Finally I got an Answer... it will always return an empty array if I want to get the data in Homepage or any other page except cart page so the solution is we make to cacheable=false is layout like in my case my layout was

Vender/Module/view/frontend/layout/default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Inchoo\ModalOverlay\Block\ModalOverlay"
                   template="Inchoo_ModalOverlay::modal_overlay.phtml"
                   name="modalOverlay"
                   as="modalOverlay" cacheable="false"/>
        </referenceContainer>
    </body>
</page>

as you can see at the end of the block I write cacheable = 'false' after just doing this simple step you'll get you cart products in every other page.

0

This code will allow you to get all items of cart.

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

Thanks

  • I tried this code your code n my code are exactly same but it returns me the null array – Asad Khan Oct 8 at 6:09
0

Your Block

public function getQuoteData()
    {
        $this->_checkoutSession->getQuote();
        if (!$this->hasData('quote')) {
            $this->setData('quote', $this->_checkoutSession->getQuote());
        }
        return $this->_getData('quote');
    }

Call the Block in your template

// Get all visible items in cart
$quote = $block->getQuoteData();

foreach($quote->getAllVisibleItems() 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 'Product Type: '.$_item->getProductType().'<br/>';
}
  • is $this->_checkoutSession is object of Magento\Checkout\Model\Session this class??? and also explain $this->_getData() as well – Asad Khan Oct 8 at 7:46
  • n the most important question is did you get the data in homepage??? – Asad Khan Oct 8 at 7:50
  • Yes you're right. $this->_getData() will get value from _data array without parsing key. – Sudhanshu Bajaj Oct 8 at 7:51
  • did you declare $this->_getData() in di??? – Asad Khan Oct 8 at 8:04
0

use the session to get the quote and your cart items.

use Magento\Checkout\Model\Session;

public function __construct(
        ...
        Session $session
        ...
    ) {
        ...
        $this->_session = $session;
        ...
    }

public function yourFunction()
{
    $items = $this->_session->getQuote()->getAllVisibleItems();
    print_r($items->getData());
}

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.