magento2 orders – How to Get the Ordered Size of a Product in Magento 2

magento2order-itemsorderssales-order

I tried to get the size of a product/products that was ordered.

So let's say a product has 5 sizes: S, M, L, XL,XLL and when i made the order the product x was bought with the size M.

When i load the order i want to get this "M" size in my code.

This is how i load my order:

$order = $_objectManager->create('Magento\Sales\Model\Order')->load($orderID);

 foreach ($order->getAllItems() as $item) {
      $product = $_objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
... here i want to get the size
}

Does anyone know how to do that? because i didn't find nothing relevant.

Thank you!

Best Answer

You can try:

$order = $_objectManager->create('Magento\Sales\Model\Order')->load($orderID);

$items = $order->getAllVisibleItems();
// $items = $order->getAllItems(); // all items


foreach ($items as $item) {
    $options = $item->getProductOptions(); // all product options       
 }

EDIT:

If you can try not to use ObjectManager directly. It defeats the purpose of dependency injection.

Related Topic