title: Magento : How to get qty of a specific product in an order item?
body: <p>I have this script to help me get the QTY of a specific product from an order item in Magento 1.6. This should be visualized in a simple table. This is my code so far:</p>
  
  <pre><code>// Load the product collection
  $collection = Mage::getModel('catalog/product')
   -&gt;getCollection()
   -&gt;addAttributeToSelect('*');
  
  foreach ($collection as $product) {
      $id = $product-&gt;getId();
      $product_name = $product-&gt;getName();
  
      $order_output = '';
      $order_output .= '&lt;table style="width: 500px"&gt;';
          $order_output .= '&lt;tr&gt;&lt;td colspan="3"&gt;&lt;strong&gt;'.$product_name.'&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;';
          $order_output .= '&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Order id&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Total price&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Qty&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Customer name&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;';
  
          // Get all unique order IDs for items with specifix product ID
          $time = time();
          $to = date('Y-m-d H:i:s', $time);
          $from = date('2012-07-01 08:00:00');
          $orderItems = Mage::getResourceModel('sales/order_item_collection')
          -&gt;addAttributeToFilter('product_id', $id)
          -&gt;addAttributeToFilter('created_at', array('from' =&gt; $from, 'to' =&gt; $to))
          -&gt;addAttributeToSelect('order_id')
          -&gt;load();
  
          foreach ($orderItems as $order) {
               $order_data = Mage::getModel('sales/order')-&gt;load($order['order_id']);
               $items = $order_data-&gt;getColletion();
               $order_id = $order['order_id'];
               $total = $order_data-&gt;getGrandTotal();
               $customer_name = $order_data-&gt;getCustomerName();
  
               foreach ($items as $item) {
                  $product_id = $item-&gt;getProductId();
                  if ($product_id == $id) {
                      $number_of_prod =  $item-&gt;getQtyOrdered();
                  }       
               }
  
               $order_output .= '&lt;/tr&gt;';
                  $order_output .= '&lt;td width="20%" align="left"&gt;'.$order_id.'&lt;/td&gt;';
                  $order_output .= '&lt;td width="20%"&gt;'.number_format($total, 2, '.', '').' RMB&lt;/td&gt;';
                  $order_output .= '&lt;td width="20%"&gt;'.$number_of_prod.'&lt;/td&gt;';
                  $order_output .= '&lt;td width="40%"&gt;'.$customer_name.'&lt;/td&gt;';
               $order_output .= '&lt;/tr&gt;';     
          }
      $order_output .= '&lt;/table&gt;';
  
      header ("Content-Type: text/html; charset=UTF-8");
      echo $order_output;
  }
  </code></pre>
  
  <p>Everything is populating correctly and the only thing missing is the QTY. You can see the attached image for the output.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p><img src="https://i.stack.imgur.com/wSlXA.png" alt="enter image description here"></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>When I use: </p>
  
  <pre><code>$items = $order_data-&gt;getAllItems();
  </code></pre>
  
  <p>I get the Qty but now all the orders of respective products is not listed.</p>