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;
}
}
0
use Magento\Checkout\Model\Session;

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

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

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.

  • Can we get quote item without customer login? – Masud Shaikh Nov 14 '18 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. – Masud Shaikh Nov 15 '18 at 6:43
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;
    }
}
  • I want to get customer cart item without customer login – Masud Shaikh Nov 15 '18 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 – Tuyen Nguyen Nov 15 '18 at 10:21
  • Can you update your post? – Masud Shaikh Nov 15 '18 at 10:25
  • Ok, I will check and update you. – Masud Shaikh Nov 15 '18 at 10:38
  • The code is not working. I have debugged $quoteItem got the message evaluate expression – Masud Shaikh Nov 15 '18 at 10:44

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.