This is my page(My_history)

enter image description here

From this page, I want to display detail of each product by each id

when I click on link View order, not just one Id is array example, I had researched in the folder, but I do not understand, can anyone help me a hand, please?

  • do you want to get product details when clicking on view order or on the current page? – Chirag Patel Jul 4 '19 at 8:31
  • I want to get product details when I clicking on view order – Kite Hany Jul 4 '19 at 8:58
  • You need to pass product id on click view order and based on that ID get an order detail – Chirag Patel Jul 4 '19 at 9:12
  • I am new to Magento so I don't know how to practice yet, Can you spend a bit your time to tell me more clear or maybe about the code – Kite Hany Jul 4 '19 at 9:19
  • Hi Kite Hany, you can have a look to this link to get the product details according to the product id : mageplaza.com/devdocs/how-get-product-id-sku-magento-2.html – Flying Finner Jul 4 '19 at 9:33

Get product collection and through that collection get product data.

$_productCollection = $block->getLoadedProductCollection();
foreach ($_productCollection as $_product):
echo $_product->getProductUrl();
// add this product url in the view order link 
  • Where should I put this code, can you show me a full code, it'll take some time, sorry but I am new to Magento so I need a help – Kite Hany Jul 4 '19 at 10:18

There are two ways to get product data by id

In your Block FIle

protected $_productFactory;

public function __construct(
    \Magento\Catalog\Model\ProductFactory $productFactory

) {
    $this->_productFactory    = $productFactory;
Public function getProductData($productId)
     $product = $this->_productFactory->create()->load($productId);
     return $product;

In your phtml File You can

$productData = $block->getProductData($productId);
  • Thank for your answer. I would like to ask, how about the .phtml file, do I need to create something in there? – Kite Hany Jul 5 '19 at 3:06
  • check my Updated answer – Waqar Ali Jul 5 '19 at 4:28
  • How can I echo the product – Kite Hany Jul 5 '19 at 7:10
  • you can echo like echo $productData->getSku(); – Waqar Ali Jul 5 '19 at 7:13
  • Sorry, I still wonder, I will choose 1 of the two of code (Using ProductFactory or Using ProductRepository) and I match the 3rd code ( block.php File) in the same block file, right? Maybe I wrong, Can you give me the full code, please, because I am new to Magento so I really need help, thanks – Kite Hany Jul 5 '19 at 7:19

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.