Question

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!

Was it helpful?

Solution

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
                    );
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top