0

How to get customer cart item collection without customer login?

I want to also get cart item through item id.

Got the null value

<?php

namespace vendor\module_name\Block\Index;

class Approveitem extends \Magento\Framework\View\Element\Template
{

    protected $_coreRegistry;


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Quote\Model\Quote\ItemFactory $quoteItemFactory,
        \Magento\Quote\Model\ResourceModel\Quote\Item $itemResourceModel
    )
    {
       $this->_coreRegistry = $coreRegistry;
       $this->quoteItemFactory = $quoteItemFactory;
       $this->itemResourceModel = $itemResourceModel;
       parent::__construct($context);
    }

    public function getCartDetails(){
        $itemId = $this->_coreRegistry->registry('itemId');

        $quoteItem = $this->quoteItemFactory->create();
        $item = $this->itemResourceModel->load($quoteItem, $itemId);

        return $item;
    }
}
  • Try to using this answer =>magento.stackexchange.com/a/241844/51810 – Rohan Hapani Nov 15 '18 at 5:36
  • Inform me is it working for you or not. – Rohan Hapani Nov 15 '18 at 5:38
  • It is not working. Got the null value. Because I am not login to the customer. I want to get the cart item data collection form database without customer login. – Masud Shaikh Nov 15 '18 at 6:31
  • Any other method so we can get the item collection without customer login not using session. @RohanHapani – Masud Shaikh Nov 15 '18 at 6:45
  • Yes. I update answer within few minute. – Rohan Hapani Nov 15 '18 at 6:46
1

Try to use this below code :

protected $cart;
protected $itemResourceModel;
protected $quoteRepository;
public function __construct(
    .......
    \Magento\Checkout\Model\Cart $cart,
    \Magento\Quote\Model\ResourceModel\Quote\Item $itemResourceModel,
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
    .......
) {
    .......
    $this->cart = $cart;
    $this->itemResourceModel = $itemResourceModel;
    $this->quoteRepository = $quoteRepository;
    .......
}
=============================

$itemsCollection = $this->cart->getQuote()->getItemsCollection();
$cartID = $this->cart->getQuote()->getId();
foreach ($itemsCollection as $item) {
    $quote = $this->quoteRepository->getActive($cartID);
    $quoteItem = $quote->getItemById($item->getItemId());
}
  • It is not working. Got the null value in item collection. – Masud Shaikh Nov 15 '18 at 7:14
  • I tested it and it's working properly without customer login. Can you please check ?I think something is not working. can you please post your whole code? – Rohan Hapani Nov 15 '18 at 7:17
  • The code is working when I add the item to the cart without customer login. – Masud Shaikh Nov 15 '18 at 7:17
  • My senario is already i have item id. I want to get the cart data using my item id – Masud Shaikh Nov 15 '18 at 7:19
  • Please check my post. I have already updated my code. I have got the item id from using the coreRegistry method. – Masud Shaikh Nov 15 '18 at 7:22

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.