2

Here is my code, where I'm calling Quote model file and getItemById function.

 public function __construct(
        \Magento\Quote\Model\Quote $quote
    ) {
        $this->quote = $quote;
    }

$itemId = "Quote Item ID";
$data = $this->quote->getItemById($itemId);

Above code returns FALSE - even there are 65 Quote Items are exists!


So I checked the Magento core method of getItemById in -

/vendor/magento/module-quote/Model/Quote.php

     /**
     * 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 I print the $this->getItemsCollection()->getData() then it returns the 65 Item arrays.

But As Magento passing directly the collection object $this->getItemsCollection() in foreach

So It's not going in-side the foreach loop which is expected result.

Is it a Magento bug or I am doing something wrong ?


Edited

Issue is still open for Magento Quote model file.

/vendor/magento/module-quote/Model/Quote.php

As Rohan Hapani's answer we're getting data from \Magento\Quote\Model\Quote\Item As for now.

But Magento's get getItemById still needs to be fixed.

/vendor/magento/module-quote/Model/Quote.php

/**
 * 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;
}

So open for the solutions :)

  • Do you have quote id? – Rohan Hapani Jan 4 at 10:15
  • 1
    No, I have Quote Item ID – Aditya Shah Jan 4 at 10:23
4

Try to use this below code :

/**
 * @var \Magento\Quote\Model\Quote\Item
 */
protected $_itemModel;

public function __construct(
    .........
    \Magento\Quote\Model\Quote\Item $itemModel
    .........
)
{
    .........
    $this->_itemModel = $itemModel;
    .........
}

public function yourFunction()
{
    $itemId = "Quote Item ID";
    $this->_itemModel->load($itemId);
}
  • But still we need to check Why Magento's Quote model function is not working! – Aditya Shah Jan 4 at 10:41
  • I suppose you are not loading the right Quote object (it is normal if you don't have quote_id) – Raul Sanchez Jan 4 at 10:48
  • No, It is. Please check the /vendor/magento/module-quote/Model/Quote.php file. – Aditya Shah Jan 4 at 10:55
  • I mean that $this->quote in your code. It is just the injected object in constructor? without loading any id? If that's the case, that object will provide the method, but it won't content any item – Raul Sanchez Jan 4 at 11:04
  • This was the actual case :) magento.stackexchange.com/questions/256705/… – Aditya Shah Jan 4 at 11:04
2

Instead of injecting \Magento\Quote\Model\Quote class in constructor, try injecting \Magento\Checkout\Model\Session and then

$quote = $this->_session->getQuote();

This assumes your code is working with a frontend active checkout session

  • Let me check @Raul – Aditya Shah Jan 4 at 10:14
  • Not working, returning false – Aditya Shah Jan 4 at 10:16
  • mmm I assume you are using an existing item_id instead of "Quote Item ID" text, right? – Raul Sanchez Jan 4 at 10:19
  • Yes yes of-course – Aditya Shah Jan 4 at 10:22
  • I have updated the answer – Raul Sanchez Jan 4 at 10:33

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.