0

I am building one pdf, which will have order details. I want to print item ordered along with its custom attributes. I have been using getAllVisibleItems() to loop through items ordered. Below is my code. How can i get custom attributes value here, like color, size, length etc.

  $items = $order->getAllVisibleItems();
    foreach($items as $item) {
                $productid = $item->getProductId();
         $productcollection = $this->productCollectionFactory- 
        >create();
         $productcollection->addFieldToSelect('color');
         $productcollection->addIdFilter($productid);
         $product = $productcollection->getItemById($item- 
       >getProductId());

        $color = $product->getData('color'); // this is a value id
        $colorLabel = $this->productHelper->productAttribute($product, 
      $color, 'color');
        $logger->info($colorLabel);
               }

Please help. How can i get color attribute value here.

1
  • Ideally, you should save the values of your product attributes on the order items. This way, if one attribute value changes after the order is placed (or if the product gets deleted), you still have access to the value when the order was placed. Maybe this can help achieve that: magento.stackexchange.com/a/205002/146. This way, you also don't need to load the full product details and cause performance issues
    – Marius
    Commented Jun 23, 2020 at 9:46

2 Answers 2

0

$item->getProduct() might not give you the fully loaded product. Use the productRepository to reload it then you can access its data with $product->getData()

3
  • this works, but it will lead to big performance issues.
    – Marius
    Commented Jun 23, 2020 at 6:49
  • godd point, might lead to some. but the registry should be able to prevent this Commented Jun 23, 2020 at 9:17
  • the product repository will not load the same product twice, but it will load different products.
    – Marius
    Commented Jun 23, 2020 at 9:44
0

Below is a snippet that will be helping you, it does use a product collection and should be simple enough for you to reuse in your own environment

class OrderHistoryItems 
{
    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
     */
    private $productCollectionFactory;
    /**
     * @var \Magento\Catalog\Helper\Output
     */
    private $productHelper;

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Helper\Output $productHelper
    ) {
        $this->productCollectionFactory = $productCollectionFactory;
        $this->productHelper = $productHelper;
    }

    public function getOrderItems(\Magento\Sales\Model\Order $order, $limit)
    {
        $productIds = [];
        foreach ($order->getAllVisibleItems() as $item) {
            $productIds[] = $item->getProductId();
        }

        $collection = $this->productCollectionFactory->create();
        $collection->addFieldToSelect('color');
        $collection->addIdFilter($productIds);

        foreach ($order->getAllVisibleItems() as $item) {
            $product = $collection->getItemById($item->getProductId());

            $color = $product->getData('color'); // this is a value id
            $colorLabel = $this->productHelper->productAttribute($product, $color, 'color');
            ..
        }


    }
}
6
  • There are multiple attributes i want to select. How can i modify $collection->addFieldToSelect('color'); to include other attributes as well
    – Nafsss
    Commented Jun 23, 2020 at 6:19
  • I believe the answer should be accepted first if it works at your end. Then of course I can adjust my answer. We get too many users complacent not giving feedback. Commented Jun 23, 2020 at 6:32
  • Sorry but your code is not working for me.
    – Nafsss
    Commented Jun 23, 2020 at 6:37
  • I have modified your code slightly. let me know if something is wrong in that
    – Nafsss
    Commented Jun 23, 2020 at 6:40
  • Thanks for the feedback, appreciated Commented Jun 23, 2020 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.