2

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 :

<?php
$OrderItemHelper = $this->helper('CompanyName\ModuleName\Helper\Orders');
$collection = $OrderItemHelper->getOrderItem();
?>
<b><i>Total order Items <?php echo count($collection)?></i></b>
<hr>
<table class="data table" id="test-data-table">
    <caption class="table-caption">Seller Product Data</caption>
    <thead>
    <tr>
        <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>

    </tr>
    </thead>
    <tbody>
    <?php foreach ($collection as $key => $data): ?>
        <tr>
            <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>

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

<?php
?>
5

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 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 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.