I'm trying to show product information on Order_View page. I have selected all of the product I have ordered from many orders to show on a page (Order_History). On the Order_History, When you click on Order Detail, it will show the product you have clicked on with information about price, qty...My idea to do that is I want to get the itemId in table sale_order_item, from that, I can get specific the product I want to show on.
Can anyone show me how can we get the itemId?? I refer to this link, But, it did not solve my problem.
<?php
namespace Magento\History\Block;
class Display extends \Magento\Framework\View\Element\Template
{
protected $order;
protected $orderRepository;
public function __construct(
\Magento\Sales\Api\Data\OrderInterface $order,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
)
{
$this->order = $order;
$this->orderRepository = $orderRepository;
}
public function yourFunction()
{
$order_increment_id = '4';
$order = $this->order->loadByIncrementId($order_increment_id);
$orderId = $order->getId();
$order = $this->orderRepository->get($orderId);
$item_data = [];
$count = 0;
foreach ($order->getAllVisibleItems() as $item) {
echo $item->getName();
$item_data[$count]['item_id'] = $item->getItemId();
$item_data[$count]['product_id'] = $item->getProductId();
$count++;
}
return $item_data;
}
}