Getting Value of product attribute on cart page even not assigned to that particular product Magento 2












8















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?



enter image description here



Please help me!!!










share|improve this question

























  • 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


















8















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?



enter image description here



Please help me!!!










share|improve this question

























  • 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
















8












8








8








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?



enter image description here



Please help me!!!










share|improve this question
















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?



enter image description here



Please help me!!!







magento2 cart sales-quote






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





















  • 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












3 Answers
3






active

oldest

votes


















2














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.






share|improve this answer
























  • Thanks for your Note suggestion

    – Rakesh Donga
    18 hours ago





















1














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






share|improve this answer








New contributor




HelgeB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




























    1














    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





    share|improve this answer
























    • Yes, I have following the same way as you mentioned and it works for me!

      – Sunny Rahevar
      20 hours ago











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer
























    • Thanks for your Note suggestion

      – Rakesh Donga
      18 hours ago


















    2














    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.






    share|improve this answer
























    • Thanks for your Note suggestion

      – Rakesh Donga
      18 hours ago
















    2












    2








    2







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    Prince PatelPrince Patel

    13.5k55077




    13.5k55077













    • 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







    Thanks for your Note suggestion

    – Rakesh Donga
    18 hours ago















    1














    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






    share|improve this answer








    New contributor




    HelgeB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      1














      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






      share|improve this answer








      New contributor




      HelgeB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        1












        1








        1







        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






        share|improve this answer








        New contributor




        HelgeB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        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







        share|improve this answer








        New contributor




        HelgeB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        HelgeB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered yesterday









        HelgeBHelgeB

        214




        214




        New contributor




        HelgeB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        HelgeB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        HelgeB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.























            1














            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





            share|improve this answer
























            • Yes, I have following the same way as you mentioned and it works for me!

              – Sunny Rahevar
              20 hours ago
















            1














            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





            share|improve this answer
























            • Yes, I have following the same way as you mentioned and it works for me!

              – Sunny Rahevar
              20 hours ago














            1












            1








            1







            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





            share|improve this answer













            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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]