1

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!

2 Answers 2

1

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.

2

Try this:

$order = $_objectManager->create('Magento\Sales\Model\Order')->load($orderID);
foreach ($order->getAllItems() as $item) {
     $productOptions = unserialize($item->getProductOptions());
     echo "<pre>";
     print_r($productOptions); 
     // check the [attributes_info] for the attributes info 
}

OR

$order = $_objectManager->create('Magento\Sales\Model\Order')->load($orderID);
foreach ($order->getAllItems() as $item) {
      $product = $item->getProduct();
      $size = $product->getSize(); // get size value
      if($product->getSize()) {
            echo $product->getAttributeText('size'); // get size label  
      }

}

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.