8

I am getting the product attribute from quote. It seems there is wrong value comes. Please check my below code.

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $cart = $objectManager->get('\Magento\Checkout\Model\Cart');
 $itemsCollection = $cart->getQuote()->getItemsCollection();
 $itemsVisible = $cart->getQuote()->getAllVisibleItems();
 $items = $cart->getQuote()->getAllItems();

 $product_object = $objectManager->create('Magento\Catalog\Model\Product');

 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!!!

  • 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 Jan 30 at 9:18
  • You need to create object $objectManager->create('Magento\Catalog\Model\Product'); in foreach loop. Check my answer – Prince Patel Jan 30 at 10:01
3

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 = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$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('Magento\Catalog\Model\Product');
       $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.

2

Please Try something like this

....

function productData($pro_id)
{   
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       $product_object = $objectManager->create('Magento\Catalog\Model\Product');
       $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 Jan 31 at 7:05
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

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.