2013-04-15 2 views
6

Magento 1.6의 주문 항목에서 특정 제품의 수량을 얻는 데 도움이되는이 스크립트가 있습니다. 이것은 간단한 표로 시각화되어야합니다. 지금까지 내 코드 :Magento : 주문 항목에서 특정 제품의 수량을 얻는 방법은 무엇입니까?

// Load the product collection 
$collection = Mage::getModel('catalog/product') 
->getCollection() 
->addAttributeToSelect('*'); 

foreach ($collection as $product) { 
    $id = $product->getId(); 
    $product_name = $product->getName(); 

    $order_output = ''; 
    $order_output .= '<table style="width: 500px">'; 
     $order_output .= '<tr><td colspan="3"><strong>'.$product_name.'</strong></td></tr>'; 
     $order_output .= '<tr><td><strong>Order id</strong></td><td><strong>Total price</strong></td><td><strong>Qty</strong></td><td><strong>Customer name</strong></td></tr>'; 

     // 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') 
     ->addAttributeToFilter('product_id', $id) 
     ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to)) 
     ->addAttributeToSelect('order_id') 
     ->load(); 

     foreach ($orderItems as $order) { 
      $order_data = Mage::getModel('sales/order')->load($order['order_id']); 
      $items = $order_data->getColletion(); 
      $order_id = $order['order_id']; 
      $total = $order_data->getGrandTotal(); 
      $customer_name = $order_data->getCustomerName(); 

      foreach ($items as $item) { 
       $product_id = $item->getProductId(); 
       if ($product_id == $id) { 
        $number_of_prod = $item->getQtyOrdered(); 
       }  
      } 

      $order_output .= '</tr>'; 
       $order_output .= '<td width="20%" align="left">'.$order_id.'</td>'; 
       $order_output .= '<td width="20%">'.number_format($total, 2, '.', '').' RMB</td>'; 
       $order_output .= '<td width="20%">'.$number_of_prod.'</td>'; 
       $order_output .= '<td width="40%">'.$customer_name.'</td>'; 
      $order_output .= '</tr>';  
     } 
    $order_output .= '</table>'; 

    header ("Content-Type: text/html; charset=UTF-8"); 
    echo $order_output; 
} 

모든 것이 올바르게 채워지고 누락 된 유일한 것이 수량입니다. 첨부 된 이미지를 볼 수 있습니다.

도움을 주시면 대단히 감사하겠습니다.

enter image description here

내가 사용하는 UPDATE :

$items = $order_data->getAllItems(); 

내가 수량을 얻을하지만 지금은 각각의 제품의 모든 주문이 표시되지 않습니다.

+0

내가 그 var에 비어 내가 생각 PRODUCT_ID $ 에코가이 라인 : $ PRODUCT_ID = $ item-> getProductId(); 잘못된 것입니다. – Ismailp

답변

17

다른 방법을 사용하여 수량을 주문 했습니까?

$item->getData('qty_ordered'); 

또는이 또 다른 간단한 함수가

$item['qty_ordered']; 
+0

네, 두 번째 솔루션은 완벽하게 작동했습니다! 감사! – Ismailp

+0

언제든지 문제가되지 않습니다! –

12

: 또한

$item->getQtyOrdered(); 
관련 문제