I am rendering sales_items rows in frontend Screenshot is: enter image description here

Can anyone please help me to find how get image path here from product id or any other solution. and set path in src tag ?

My .phtml file code is there :

$OrderItemHelper = $this->helper('CompanyName\ModuleName\Helper\Orders');
$collection = $OrderItemHelper->getOrderItem();
<b><i>Total order Items <?php echo count($collection)?></i></b>
<table class="data table" id="test-data-table">
    <caption class="table-caption">Seller Product Data</caption>
        <th scope="col" class="col title">ID</th>
        <th scope="col" class="col title">Product Id</th>
        <th scope="col" class="col title">Image</th>
        <th scope="col" class="col author">Name</th>
        <th scope="col" class="col author">Sku</th>
        <th scope="col" class="col author">Description</th>
        <th scope="col" class="col author">Sale Date</th>
        <th scope="col" class="col author">Price</th>
        <th scope="col" class="col author">Qty</th>
        <th scope="col" class="col author">Total</th>
        <th scope="col" class="col author">Action</th>

    <?php foreach ($collection as $key => $data): ?>
            <td data-th="ID" ><?php echo $data->getItemId(); ?></td>
            <td data-th="ID" ><?php echo $data->getProductId(); ?></td>
            <td data-th="Image" ><img src="how can get path here" ></td>
            <td data-th="Name" ><?php echo $data->getName(); ?></td>
            <td data-th="Sku"><?php echo $data->getSku(); ?></td>
            <td data-th="Description"><?php echo $data->getDescription(); ?></td>
            <td data-th="CreatedAt"><?php echo $data->getCreatedAt(); ?></td>
            <td data-th="Price"><?php echo $data->getPrice(); ?></td>
            <td data-th="Sku"><?php echo $data->getQtyOrdered(); ?></td>
            <td data-th="Sku"><?php echo ($data->getQtyOrdered()*$data->getPrice()); ?></td>

                <a href="<?php echo $block->getBaseUrl().'admin/catalog/product/edit/id/'.$data->getId(); ?>" class="action view">
    <?php endforeach; ?>


First, you load product using product id like

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($data->getProductId());

After that, get the URL of the image

$imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
$image_url = $imageHelper->init($product, 'product_base_image')->getUrl();

You set on this URL in your code like

<td data-th="Image" ><img src="<?php echo $image_url; ?>" ></td>
  • let me check please – HaFiz Umer Jul 6 '19 at 8:03
  • Yes Thank You is absolute work ! before your answer i was going to try get path from db use Join between catalog_product_entity_media_gallery_value and catalog_product_entity_media_gallery table . i.e catalog_product_entity_media_gallery_value.entity_id=catalog_product_entity_media_gallery.value_id . – HaFiz Umer Jul 6 '19 at 8:16

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.