2013-04-15 39 views
6

Tôi có tập lệnh này để giúp tôi nhận được QTY của một sản phẩm cụ thể từ một đơn đặt hàng trong Magento 1.6. Điều này nên được hiển thị trong một bảng đơn giản. Đây là mã của tôi cho đến thời điểm này:Magento: Cách nhận số lượng của một sản phẩm cụ thể trong một đơn đặt hàng?

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

Mọi thứ đều được điền chính xác và điều duy nhất bị thiếu là QTY. Bạn có thể xem hình ảnh đính kèm cho đầu ra.

Bất kỳ trợ giúp nào được đánh giá cao.

enter image description here

CẬP NHẬT

Khi tôi sử dụng:

$items = $order_data->getAllItems(); 

tôi nhận được Số lượng nhưng bây giờ tất cả các đơn đặt hàng các sản phẩm tương ứng không được liệt kê.

+0

Ngoài ra khi tôi echo $ product_id var đó là trống rỗng vì vậy tôi đoán rằng dòng này: $ product_id = $ item-> getProductId(); là sai. – Ismailp

Trả lời

17

Bạn đã thử các phương pháp thay thế để yêu cầu số lượng chưa?

$item->getData('qty_ordered'); 

hoặc

$item['qty_ordered']; 
+0

Vâng, giải pháp thứ hai hoạt động hoàn hảo! Cảm ơn! – Ismailp

+0

Không sao cả! –

12

Có một chức năng đơn giản cho việc này:

$item->getQtyOrdered(); 
Các vấn đề liên quan