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 :)