Question

I am trying to get a list of products in the basket. I have the following in my class

 public function __construct(\Magento\Quote\Api\Data\CartInterface $cartInterface) 
 {
        $this->cartInterface = $cartInterface;
 }

 public function getCart()
 {

    $cartId = $this->cartInterface->getId();

    echo $cartId;
 }

The issue I have is that the $cartId is always coming back as null even though I have 3 items in my basket

Please, could someone advise what I am doing wrong?

I use to use \Magento\Checkout\Model\Cart but this has now been deprecated

No correct solution

OTHER TIPS

You can get all quote items for current logged in customer from quote.

protected $quoteFactory;
protected $quoteModel;

public function __construct(
    \Magento\Quote\Model\QuoteFactory $quoteFactory,
    \Magento\Quote\Model\ResourceModel\Quote $quoteModel
) {
    $this->quoteFactory = $quoteFactory;
    $this->quoteModel=$quoteModel;
}

$quote = $this->quoteFactory->create();
$customerQuote = $this->quoteModel->loadByCustomerId($quote,$customerId);
$items = $customerQuote->getAllItems();

Hope it helps!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top