0

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;
}

}

1
  • Can you please attach screenshot or sales_order_item table? Commented Jul 4, 2019 at 4:45

3 Answers 3

4

You can use this below code and get order item id using order id :

Method 1 :

protected $orderRepository;

public function __construct(
    ...
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    ....
) {
    ....
    $this->orderRepository = $orderRepository;
    ....
}

public function yourFunction()
{
    $orderId = 105105;
    $order = $this->orderRepository->get($orderId);
    $item_data = [];
    $count = 0;
    foreach ($order->getAllVisibleItems() as $item) {
        //var_dump($item->getData());
        $item_data[$count]['item_id'] = $item->getItemId();
        $item_data[$count]['product_id'] = $item->getProductId(); //for product Id
        $count++;
    }
    return $item_data;
}

Method 2 :

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 = '100005363';
    $order = $this->order->loadByIncrementId($order_increment_id);
    $orderId = $order->getId();
    $order = $this->orderRepository->get($orderId);
    $item_data = [];
    $count = 0;
    foreach ($order->getAllVisibleItems() as $item) {
        //var_dump($item->getData());
        $item_data[$count]['item_id'] = $item->getItemId();
        $item_data[$count]['product_id'] = $item->getProductId(); //for product Id
        $count++;

    }
    return $item_data;
}

Method 1 : Get order item id by order id

Method 2 : Get order item id by order increment id

Now, call function $block->yourFunction() in your phtml file.

10
  • Hi @Rohan, Thanks to you a lot. I get this error after followed your code: An ID is needed. Set the ID and try again. Commented Jul 4, 2019 at 6:48
  • Which id is needed? Which method you used? Commented Jul 4, 2019 at 6:58
  • I used method 2 Commented Jul 4, 2019 at 7:01
  • Can you please upload your code in your question which you tried? I think you didn't comment simple text of comment so. Commented Jul 4, 2019 at 7:08
  • I'm so sorry but I have no idea. I just used your code and run, and get the error is: Exception #0 (Magento\Framework\Exception\InputException): An ID is needed. Set the ID and try again. Commented Jul 4, 2019 at 7:08
3

If you have order id then you can load by this:

//Where $this->orderRepository is the instance of \Magento\Sales\Api\OrderRepositoryInterface
$order = $this->orderRepository->get($id);

If you have order increment id then you can load order by this:

//Where $this->orderInterfaceFactory is the instance of \Magento\Sales\Api\Data\OrderInterfaceFactory
$order = $this->orderInterfaceFactory->create()->loadByIncrementId($orderId);

Now get all items

$items = $order->getAllVisibleItems();

The difference between getAllVisibleItems() and getAllItems() is that Visible items will only show items which would visible on frontend, whereas AllItems will send visible as well as associated items.

Now use foreach loop to get item ids

foreach($items as $item)
{
  echo "Order Item ID :".$item->getItemId();
  echo "Order Product ID : ".$item->getProductId();
}

$item->getItemId() will return item id which is primary key of sale_order_item table, whereas $item->getProductId() will return product id.

1
  • 1
    Thanks @ShoaibMunir +1 for mine also :) Commented Jul 4, 2019 at 9:22
1

If you just need to get the order_item_id for an item quickly:

  1. Pull the order up in the Magento Admin
  2. Inspecting the product in the "Items Ordered" section
  3. In the html, you should see the ID (in the below screenshot it is 140)

This worked for me when I was in a pinch; hope it helps you!

enter image description here

1
  • You saved 10 mins :)
    – anonymous
    Commented Oct 6, 2021 at 17:24

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.