0

I want to get quote item through the item id on the guest page.

Got the null value

<?php
namespace Codism\Csr\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 getCustomerCartDetails(){
    $itemId = $this->_coreRegistry->registry('itemId');

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

    return $item;
}
}

3 Answers 3

1

You can get by following code.

public function __construct(
  \Magento\Quote\Model\Quote\ItemFactory $quoteItemFactory,
  \Magento\Quote\Model\ResourceModel\Quote\Item $itemResourceModel
) {
   $this->quoteItemFactory = $quoteItemFactory;
   $this->itemResourceModel = $itemResourceModel

}

Below is code for your logic

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

This code is given here in answer.

2
  • Can we get quote item without customer login? Commented Nov 14, 2018 at 16:08
  • It is not working. Got the null value. I want to to get cart item collection from the database without customer login. Commented Nov 15, 2018 at 6:43
0
use Magento\Checkout\Model\Session;

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

public function yourCustomFunction()
{
    $items = $this->_session->getQuote()->getAllVisibleItems();
}
3
  • I want to get quote item details on the guest page not after login. Commented Nov 14, 2018 at 14:34
  • the above code is to get quote items data from session. so it is working on all magento page. Commented Nov 14, 2018 at 14:35
  • Got the Fatal error: Uncaught Error: Call to a member function getAllVisibleItems() Commented Nov 14, 2018 at 14:44
0

Using getItemById from Quote Model

https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Quote/Model/Quote.php#L1478

/**
 * Retrieve item model object by item identifier
 *
 * @param   int $itemId
 * @return  \Magento\Quote\Model\Quote\Item|false
 */
public function getItemById($itemId)
{
    foreach ($this->getItemsCollection() as $item) {
        if ($item->getId() == $itemId) {
            return $item;
        }
    }
    return false;
}

If you have quote

$quoteItem = $quote->getItemById($itemId);

For example

<?php

namespace Codism\Csr\Block\Index;

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

    /**
     * @var \Magento\Backend\Model\Session\Quote
     */
    protected $_sessionQuote;

    protected $_coreRegistry;


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Backend\Model\Session\Quote $sessionQuote
    )
    {
        $this->_coreRegistry = $coreRegistry;
        $this->_sessionQuote = $sessionQuote;
        parent::__construct($context);
    }

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

        /** @var $quote \Magento\Quote\Model\Quote */
        $quote = $this->_sessionQuote->getQuote();

        if($itemId) {
            $quoteItem = $quote->getItemById(intval($itemId));

            return $quoteItem;
        }

        return null;
    }
}
8
  • I want to get customer cart item without customer login Commented Nov 15, 2018 at 10:13
  • Using Magento\Backend\Model\Session\Quote $quote = $quoteSession->getQuote(); To get current quote, then you could get the $quoteItem without caring whether customers logged in or not Commented Nov 15, 2018 at 10:21
  • Can you update your post? Commented Nov 15, 2018 at 10:25
  • Ok, I will check and update you. Commented Nov 15, 2018 at 10:38
  • The code is not working. I have debugged $quoteItem got the message evaluate expression Commented Nov 15, 2018 at 10:44

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.