0

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?

6
  • do you want to get product details when clicking on view order or on the current page? Commented Jul 4, 2019 at 8:31
  • I want to get product details when I clicking on view order
    – Kite Hany
    Commented Jul 4, 2019 at 8:58
  • You need to pass product id on click view order and based on that ID get an order detail Commented Jul 4, 2019 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
    Commented Jul 4, 2019 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 Commented Jul 4, 2019 at 9:33

2 Answers 2

0

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 
endforeach;
1
  • 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
    Commented Jul 4, 2019 at 10:18
0

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);
9
  • Thank for your answer. I would like to ask, how about the .phtml file, do I need to create something in there?
    – Kite Hany
    Commented Jul 5, 2019 at 3:06
  • check my Updated answer
    – Waqar Ali
    Commented Jul 5, 2019 at 4:28
  • How can I echo the product
    – Kite Hany
    Commented Jul 5, 2019 at 7:10
  • you can echo like echo $productData->getSku();
    – Waqar Ali
    Commented Jul 5, 2019 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
    Commented Jul 5, 2019 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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