Getting Value of product attribute on cart page even not assigned to that particular product Magento 2
I am getting the product attribute from quote. It seems there is wrong value comes. Please check my below code.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
foreach($itemsVisible as $item){
if($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
$item_s = $product_object->load($productId);
echo $screen_size = $productId."/".$item_s->getScreenFrameSize()."/".$item_s->getFiberglassScreenRollSize()."/".$item_s->getScreenCornerSize()."<br>";
}
}
Please check below image for better understanding. Is there any error in my collection or loop?
Please help me!!!
magento2 cart sales-quote
add a comment |
I am getting the product attribute from quote. It seems there is wrong value comes. Please check my below code.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
foreach($itemsVisible as $item){
if($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
$item_s = $product_object->load($productId);
echo $screen_size = $productId."/".$item_s->getScreenFrameSize()."/".$item_s->getFiberglassScreenRollSize()."/".$item_s->getScreenCornerSize()."<br>";
}
}
Please check below image for better understanding. Is there any error in my collection or loop?
Please help me!!!
magento2 cart sales-quote
I have use this unset function before $item_s = $product_object->load($productId);. No luck!! Is there any issue in my code? i don't know why this is happening!!
– Sunny Rahevar
yesterday
You need to create object$objectManager->create('MagentoCatalogModelProduct');
in foreach loop. Check my answer
– Prince Patel
yesterday
add a comment |
I am getting the product attribute from quote. It seems there is wrong value comes. Please check my below code.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
foreach($itemsVisible as $item){
if($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
$item_s = $product_object->load($productId);
echo $screen_size = $productId."/".$item_s->getScreenFrameSize()."/".$item_s->getFiberglassScreenRollSize()."/".$item_s->getScreenCornerSize()."<br>";
}
}
Please check below image for better understanding. Is there any error in my collection or loop?
Please help me!!!
magento2 cart sales-quote
I am getting the product attribute from quote. It seems there is wrong value comes. Please check my below code.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
foreach($itemsVisible as $item){
if($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
$item_s = $product_object->load($productId);
echo $screen_size = $productId."/".$item_s->getScreenFrameSize()."/".$item_s->getFiberglassScreenRollSize()."/".$item_s->getScreenCornerSize()."<br>";
}
}
Please check below image for better understanding. Is there any error in my collection or loop?
Please help me!!!
magento2 cart sales-quote
magento2 cart sales-quote
edited yesterday
Divyarajsinh
382111
382111
asked yesterday
Sunny RahevarSunny Rahevar
1,007112
1,007112
I have use this unset function before $item_s = $product_object->load($productId);. No luck!! Is there any issue in my code? i don't know why this is happening!!
– Sunny Rahevar
yesterday
You need to create object$objectManager->create('MagentoCatalogModelProduct');
in foreach loop. Check my answer
– Prince Patel
yesterday
add a comment |
I have use this unset function before $item_s = $product_object->load($productId);. No luck!! Is there any issue in my code? i don't know why this is happening!!
– Sunny Rahevar
yesterday
You need to create object$objectManager->create('MagentoCatalogModelProduct');
in foreach loop. Check my answer
– Prince Patel
yesterday
I have use this unset function before $item_s = $product_object->load($productId);. No luck!! Is there any issue in my code? i don't know why this is happening!!
– Sunny Rahevar
yesterday
I have use this unset function before $item_s = $product_object->load($productId);. No luck!! Is there any issue in my code? i don't know why this is happening!!
– Sunny Rahevar
yesterday
You need to create object
$objectManager->create('MagentoCatalogModelProduct');
in foreach loop. Check my answer– Prince Patel
yesterday
You need to create object
$objectManager->create('MagentoCatalogModelProduct');
in foreach loop. Check my answer– Prince Patel
yesterday
add a comment |
3 Answers
3
active
oldest
votes
Becuase you use same object every time in foreach. YOu need to create new object in foreach loop. So you final code look like this:
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
foreach($itemsVisible as $item){
if($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($productId);
echo $screen_size = $productId."/".$item_s->getScreenFrameSize()."/".$item_s->getFiberglassScreenRollSize()."/".$item_s->getScreenCornerSize()."<br>";
}
}
NOTE: Don't use object manager directly in code. Use product factory becuase factory will create new object every time.
Thanks for your Note suggestion
– Rakesh Donga
18 hours ago
add a comment |
I guess you have to create a new product object instead of reusing it in your foreach loop. By reusing the product object you can get such side effects even if the method "load" suggests, that every data would be overwritten.
Check the usage of custom attributes in AbstractExtensibleModel
New contributor
add a comment |
Please Try something like this
....
function productData($pro_id)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($pro_id);
return $item_s;
}
....
$item_s = productData($productId); //In foreach loop
Yes, I have following the same way as you mentioned and it works for me!
– Sunny Rahevar
20 hours ago
add a comment |
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%2f259769%2fgetting-value-of-product-attribute-on-cart-page-even-not-assigned-to-that-partic%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Becuase you use same object every time in foreach. YOu need to create new object in foreach loop. So you final code look like this:
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
foreach($itemsVisible as $item){
if($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($productId);
echo $screen_size = $productId."/".$item_s->getScreenFrameSize()."/".$item_s->getFiberglassScreenRollSize()."/".$item_s->getScreenCornerSize()."<br>";
}
}
NOTE: Don't use object manager directly in code. Use product factory becuase factory will create new object every time.
Thanks for your Note suggestion
– Rakesh Donga
18 hours ago
add a comment |
Becuase you use same object every time in foreach. YOu need to create new object in foreach loop. So you final code look like this:
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
foreach($itemsVisible as $item){
if($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($productId);
echo $screen_size = $productId."/".$item_s->getScreenFrameSize()."/".$item_s->getFiberglassScreenRollSize()."/".$item_s->getScreenCornerSize()."<br>";
}
}
NOTE: Don't use object manager directly in code. Use product factory becuase factory will create new object every time.
Thanks for your Note suggestion
– Rakesh Donga
18 hours ago
add a comment |
Becuase you use same object every time in foreach. YOu need to create new object in foreach loop. So you final code look like this:
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
foreach($itemsVisible as $item){
if($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($productId);
echo $screen_size = $productId."/".$item_s->getScreenFrameSize()."/".$item_s->getFiberglassScreenRollSize()."/".$item_s->getScreenCornerSize()."<br>";
}
}
NOTE: Don't use object manager directly in code. Use product factory becuase factory will create new object every time.
Becuase you use same object every time in foreach. YOu need to create new object in foreach loop. So you final code look like this:
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
foreach($itemsVisible as $item){
if($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($productId);
echo $screen_size = $productId."/".$item_s->getScreenFrameSize()."/".$item_s->getFiberglassScreenRollSize()."/".$item_s->getScreenCornerSize()."<br>";
}
}
NOTE: Don't use object manager directly in code. Use product factory becuase factory will create new object every time.
answered yesterday
Prince PatelPrince Patel
13.5k55077
13.5k55077
Thanks for your Note suggestion
– Rakesh Donga
18 hours ago
add a comment |
Thanks for your Note suggestion
– Rakesh Donga
18 hours ago
Thanks for your Note suggestion
– Rakesh Donga
18 hours ago
Thanks for your Note suggestion
– Rakesh Donga
18 hours ago
add a comment |
I guess you have to create a new product object instead of reusing it in your foreach loop. By reusing the product object you can get such side effects even if the method "load" suggests, that every data would be overwritten.
Check the usage of custom attributes in AbstractExtensibleModel
New contributor
add a comment |
I guess you have to create a new product object instead of reusing it in your foreach loop. By reusing the product object you can get such side effects even if the method "load" suggests, that every data would be overwritten.
Check the usage of custom attributes in AbstractExtensibleModel
New contributor
add a comment |
I guess you have to create a new product object instead of reusing it in your foreach loop. By reusing the product object you can get such side effects even if the method "load" suggests, that every data would be overwritten.
Check the usage of custom attributes in AbstractExtensibleModel
New contributor
I guess you have to create a new product object instead of reusing it in your foreach loop. By reusing the product object you can get such side effects even if the method "load" suggests, that every data would be overwritten.
Check the usage of custom attributes in AbstractExtensibleModel
New contributor
New contributor
answered yesterday
HelgeBHelgeB
214
214
New contributor
New contributor
add a comment |
add a comment |
Please Try something like this
....
function productData($pro_id)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($pro_id);
return $item_s;
}
....
$item_s = productData($productId); //In foreach loop
Yes, I have following the same way as you mentioned and it works for me!
– Sunny Rahevar
20 hours ago
add a comment |
Please Try something like this
....
function productData($pro_id)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($pro_id);
return $item_s;
}
....
$item_s = productData($productId); //In foreach loop
Yes, I have following the same way as you mentioned and it works for me!
– Sunny Rahevar
20 hours ago
add a comment |
Please Try something like this
....
function productData($pro_id)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($pro_id);
return $item_s;
}
....
$item_s = productData($productId); //In foreach loop
Please Try something like this
....
function productData($pro_id)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product_object = $objectManager->create('MagentoCatalogModelProduct');
$item_s = $product_object->load($pro_id);
return $item_s;
}
....
$item_s = productData($productId); //In foreach loop
answered 21 hours ago
DivyarajsinhDivyarajsinh
382111
382111
Yes, I have following the same way as you mentioned and it works for me!
– Sunny Rahevar
20 hours ago
add a comment |
Yes, I have following the same way as you mentioned and it works for me!
– Sunny Rahevar
20 hours ago
Yes, I have following the same way as you mentioned and it works for me!
– Sunny Rahevar
20 hours ago
Yes, I have following the same way as you mentioned and it works for me!
– Sunny Rahevar
20 hours ago
add a comment |
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.
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%2f259769%2fgetting-value-of-product-attribute-on-cart-page-even-not-assigned-to-that-partic%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
I have use this unset function before $item_s = $product_object->load($productId);. No luck!! Is there any issue in my code? i don't know why this is happening!!
– Sunny Rahevar
yesterday
You need to create object
$objectManager->create('MagentoCatalogModelProduct');
in foreach loop. Check my answer– Prince Patel
yesterday