I'm trying to show product detail by order Item_Id. I have this page:

enter image description here

When we click on Order Detail, it will get the item (2) to show product detail we've chosen. Here is my code to show this page:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $orders        = $objectManager-> 

    echo "<p style='color:red; 
             Product Ordered
  <div class="table-wrapper orders-history">
       <table class="data table table-order-items history" id="my-orders- 
        <caption class="table-caption"><?= $block->escapeHtml(__('Orders')) 
      <th scope="col" class="col orderitemid"> <?= $block->escapeHtml(__('OrderItem'    )) ?></th>
      <th scope="col" class="col image      "> <?= $block->escapeHtml(__('Product Image')) ?></th>
      <th scope="col" class="col name       "> <?= $block->escapeHtml(__('Product Name' )) ?></th>
      <th scope="col" class="col Qty        "> <?= $block->escapeHtml(__('Qty'          )) ?></th>
      <th scope="col" class="col Price      "> <?= $block->escapeHtml(__('Price'        )) ?></th>
      <th scope="col" class="col Total      "> <?= $block->escapeHtml(__('Total'        )) ?></th>
      <th scope="col" class="col Action     "> <?= $block->escapeHtml(__('Action'       )) ?></th>
  <?php foreach($orders as $item): ?>
     $_product     = $objectManager->get('Magento\Catalog\Model\Product')->load($item->getProductId());
     $imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
     $image_url    = $imageHelper->init($_product, 'product_page_image_small')->setImageFile($_product->getImage())->resize(100, 100)->getUrl();
        <td data-th="OrderItem    "class="col orderitemid">  <?php echo $item->getItemId();           ?></td>
        <td data-th="Product Image"class="col image   ">  <img src="<?= $image_url ?>"  width="75px" height="75px"></td>
        <td data-th="Product Name "class="col name    ">  <?php echo $item->getName();                ?></td>
        <td data-th="Qty          "class="col qty     ">  <?php echo $item->getQtyOrdered();          ?></td>
        <td data-th="Price        "class="col price   ">  <?php echo $item->getPrice();               ?></td>
        <td data-th="Subtotal     "class="col subtotal">  <?php echo $item->getBaseRowTotalInclTax(); ?></td>

        <td data-th="<?= $block->escapeHtml(__('Actions')) ?>" class="col actions">
          <a href="<?php echo $this->getUrl('history/detail/display',['item_id' => $item->getItemId()]); ?>">Order Detail</a>
  <?php endforeach; ?>

Can anyone help the next step? Thanks a lot.

  • You want to redirect to product page? – Rohan Hapani Jul 11 '19 at 8:55
  • Hi @Rohan. Could you tell me how can I get productUrl to redirect to product page?? – Michael Tao Jul 17 '19 at 9:09
  • Please check my answer. using that you can get product URL. – Rohan Hapani Jul 17 '19 at 9:12

Remove this code :

<?php echo $this->getUrl('history/detail/display',['item_id' => $item->getItemId()]); ?>

And add this code for redirect to product page :

<?php echo $_product->getProductUrl(); ?>
| improve this answer | |
  • Thanks for your answer @Rohan. How about the file .phtml I want to show the product – Michael Tao Jul 11 '19 at 9:09
  • As I said, It will redirect to product page. – Rohan Hapani Jul 11 '19 at 9:10

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.