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

magento.stackexchange https://magento.stackexchange.com/questions/259769

  •  16-02-2021
  •  | 
  •  

سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top