0

I have one order with 5 products. 3 products are uniq product id. 2 products has same product id but product option are different.

$order_id = 100;
$order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($order_id);

$orderedItems = $order->getAllVisibleItems();
$orderedProductIds = [];
foreach ($orderedItems as $item) {
        $orderedProductIds[] = $item->getData('product_id');
}
$collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*');

    $collection->getSelect()->join(['item' => $this->_resource->getTableName('sales_order_item')],
        'e.entity_id = item.product_id',
        [
            'sku'=>'item.sku',
            'qty'=>'item.qty_ordered',
            'name'=>'item.name',
            'price'=>'item.price_incl_tax',
            'order_id'=>'item.order_id',
            'product_type'=>'item.product_type',
            'base_price'=>'item.price_incl_tax',
        ]
        )->where(
            'item.order_id=?',
            $order_id
        );
        $collection->addFieldToFilter('entity_id', ['in' => $orderedProductIds]);
 if($collection->getSize())
 {
   $data = $collection->getFirstItem();
 }

When i try to get FirstItem using above code its give error Item (Magento\Catalog\Model\Product\Interceptor) with the same ID "productid" already exists.";

| improve this question | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.