getItemById($itemId) for Magento Quote Model not working as expected in Magento 2
Here is my code, where I'm calling Quote model file and getItemById
function.
public function __construct(
MagentoQuoteModelQuote $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 MagentoQuoteModelQuoteItem|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 MagentoQuoteModelQuoteItem
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 MagentoQuoteModelQuoteItem|false
*/
public function getItemById($itemId)
{
foreach ($this->getItemsCollection() as $item) {
if ($item->getId() == $itemId) {
return $item;
}
}
return false;
}
So open for the solutions :)
magento2 model quote bug quoteitem
add a comment |
Here is my code, where I'm calling Quote model file and getItemById
function.
public function __construct(
MagentoQuoteModelQuote $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 MagentoQuoteModelQuoteItem|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 MagentoQuoteModelQuoteItem
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 MagentoQuoteModelQuoteItem|false
*/
public function getItemById($itemId)
{
foreach ($this->getItemsCollection() as $item) {
if ($item->getId() == $itemId) {
return $item;
}
}
return false;
}
So open for the solutions :)
magento2 model quote bug quoteitem
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
add a comment |
Here is my code, where I'm calling Quote model file and getItemById
function.
public function __construct(
MagentoQuoteModelQuote $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 MagentoQuoteModelQuoteItem|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 MagentoQuoteModelQuoteItem
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 MagentoQuoteModelQuoteItem|false
*/
public function getItemById($itemId)
{
foreach ($this->getItemsCollection() as $item) {
if ($item->getId() == $itemId) {
return $item;
}
}
return false;
}
So open for the solutions :)
magento2 model quote bug quoteitem
Here is my code, where I'm calling Quote model file and getItemById
function.
public function __construct(
MagentoQuoteModelQuote $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 MagentoQuoteModelQuoteItem|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 MagentoQuoteModelQuoteItem
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 MagentoQuoteModelQuoteItem|false
*/
public function getItemById($itemId)
{
foreach ($this->getItemsCollection() as $item) {
if ($item->getId() == $itemId) {
return $item;
}
}
return false;
}
So open for the solutions :)
magento2 model quote bug quoteitem
magento2 model quote bug quoteitem
edited Jan 4 at 18:31
Aditya Shah
asked Jan 4 at 10:03
Aditya ShahAditya Shah
3,6202834
3,6202834
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
add a comment |
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
Do you have quote id?
– Rohan Hapani
Jan 4 at 10:15
Do you have quote id?
– Rohan Hapani
Jan 4 at 10:15
1
1
No, I have Quote Item ID
– Aditya Shah
Jan 4 at 10:23
No, I have Quote Item ID
– Aditya Shah
Jan 4 at 10:23
add a comment |
2 Answers
2
active
oldest
votes
Try to use this below code :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
public function __construct(
.........
MagentoQuoteModelQuoteItem $itemModel
.........
)
{
.........
$this->_itemModel = $itemModel;
.........
}
public function yourFunction()
{
$itemId = "Quote Item ID";
$this->_itemModel->load($itemId);
}
1
Worked like a charm! Thanks bro :)
– Aditya Shah
Jan 4 at 10:40
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
|
show 2 more comments
Instead of injecting MagentoQuoteModelQuote class in constructor, try injecting MagentoCheckoutModelSession 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
|
show 2 more comments
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256754%2fgetitembyiditemid-for-magento-quote-model-not-working-as-expected-in-magento%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try to use this below code :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
public function __construct(
.........
MagentoQuoteModelQuoteItem $itemModel
.........
)
{
.........
$this->_itemModel = $itemModel;
.........
}
public function yourFunction()
{
$itemId = "Quote Item ID";
$this->_itemModel->load($itemId);
}
1
Worked like a charm! Thanks bro :)
– Aditya Shah
Jan 4 at 10:40
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
|
show 2 more comments
Try to use this below code :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
public function __construct(
.........
MagentoQuoteModelQuoteItem $itemModel
.........
)
{
.........
$this->_itemModel = $itemModel;
.........
}
public function yourFunction()
{
$itemId = "Quote Item ID";
$this->_itemModel->load($itemId);
}
1
Worked like a charm! Thanks bro :)
– Aditya Shah
Jan 4 at 10:40
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
|
show 2 more comments
Try to use this below code :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
public function __construct(
.........
MagentoQuoteModelQuoteItem $itemModel
.........
)
{
.........
$this->_itemModel = $itemModel;
.........
}
public function yourFunction()
{
$itemId = "Quote Item ID";
$this->_itemModel->load($itemId);
}
Try to use this below code :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
public function __construct(
.........
MagentoQuoteModelQuoteItem $itemModel
.........
)
{
.........
$this->_itemModel = $itemModel;
.........
}
public function yourFunction()
{
$itemId = "Quote Item ID";
$this->_itemModel->load($itemId);
}
answered Jan 4 at 10:26
Rohan HapaniRohan Hapani
5,85721662
5,85721662
1
Worked like a charm! Thanks bro :)
– Aditya Shah
Jan 4 at 10:40
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
|
show 2 more comments
1
Worked like a charm! Thanks bro :)
– Aditya Shah
Jan 4 at 10:40
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
1
1
Worked like a charm! Thanks bro :)
– Aditya Shah
Jan 4 at 10:40
Worked like a charm! Thanks bro :)
– Aditya Shah
Jan 4 at 10:40
But still we need to check Why Magento's Quote model function is not working!
– Aditya Shah
Jan 4 at 10:41
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
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
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
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
|
show 2 more comments
Instead of injecting MagentoQuoteModelQuote class in constructor, try injecting MagentoCheckoutModelSession 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
|
show 2 more comments
Instead of injecting MagentoQuoteModelQuote class in constructor, try injecting MagentoCheckoutModelSession 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
|
show 2 more comments
Instead of injecting MagentoQuoteModelQuote class in constructor, try injecting MagentoCheckoutModelSession and then
$quote = $this->_session->getQuote();
This assumes your code is working with a frontend active checkout session
Instead of injecting MagentoQuoteModelQuote class in constructor, try injecting MagentoCheckoutModelSession and then
$quote = $this->_session->getQuote();
This assumes your code is working with a frontend active checkout session
edited Jan 4 at 10:32
answered Jan 4 at 10:12
Raul SanchezRaul Sanchez
1,84731135
1,84731135
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
|
show 2 more comments
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
Let me check @Raul
– Aditya Shah
Jan 4 at 10:14
Let me check @Raul
– Aditya Shah
Jan 4 at 10:14
Not working, returning false
– Aditya Shah
Jan 4 at 10:16
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
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
Yes yes of-course
– Aditya Shah
Jan 4 at 10:22
I have updated the answer
– Raul Sanchez
Jan 4 at 10:33
I have updated the answer
– Raul Sanchez
Jan 4 at 10:33
|
show 2 more comments
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256754%2fgetitembyiditemid-for-magento-quote-model-not-working-as-expected-in-magento%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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