Magento 2.3.0 How to get product image from order item collection

by Chirag Patel   Last Updated April 15, 2019 10:09 AM

I would like to get a product image from order items collection. How can I get a product thumbnail image?

Below is my custom order item collection code :

public function getEmailLogOrders() {
   $logCollection =  $this->customColectionFactory->create()->getCollection();
   $logCollection->addFieldToFilter('mail_status',array('eq' => 1));

   foreach($logCollection as $logItem){
        /*** Order Increment id's ***/
        $orderID = $logItem->getOrderNumber();
        /*** Load order items by Increment ID's ***/            
        $orders = $this->order->loadByIncrementId($orderID);
        $items = $orders->getAllItems();
        $itemQty = array();
        /*** loop for get all order items ***/
        foreach ($items as $oItem) {
           $itemQty[]=array(
               'quantity'=>$oItem->getQtyOrdered(),                   
               'name'=>$oItem->getName()                   
            );
        }
    } //echo '<pre>';print_r($itemQty);die;
    return $itemQty;     
}

Using the above code I am able to get Product Name & Qty. But I just want to know how can I get a product image within this collection.

Above collection, OutPut is like below.

Array
(
[0] => Array
    (
        [quantity] => 1.0000
        [name] => Push It Messenger Bag
        //Image parameter will be here
    )

[1] => Array
    (
        [quantity] => 1.0000
        [name] => Driven Backpack
        //Image parameter will be here
    )

)

Any help would be appreciated!Thanks.



Answers 1


Using catalog image helper you can got that result.

private $imgHelper;

public function __construct(\Magento\Catalog\Helper\Image $imageHelper)
{
    $this->imgHelper = $imageHelper;
}

public function getEmailLogOrders() {
    /*** loop for get all order items ***/
    foreach ($items as $oItem) {
        $imageUrl = $this->imgHelper->init($oItem->getProduct(), 'small_image',['type'=>'small_image'])->keepAspectRatio(true)->resize('75', '75')->getUrl();

        $itemQty[]= array(
                        'quantity'=>$oItem->getQtyOrdered(),
                        'name'=>$oItem->getName(),
                        'product_image' => $imageUrl
                    );
    }
}
Himanshu
Himanshu
April 15, 2019 09:54 AM

Related Questions


Updated February 22, 2017 12:09 PM

Updated April 16, 2019 14:09 PM

Updated September 10, 2017 10:09 AM

Updated February 15, 2019 10:09 AM