1

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!

2

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
                    );
    }
}
  • Tnxs for the answer. i will try and let you know :) – Chirag Patel Apr 15 at 10:00
  • 1
    You are a genius. it's working like a charm Thanks :) – Chirag Patel Apr 15 at 10:08

Your Answer

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

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