2013-04-15 7 views
6

Ho questo script per aiutarmi a ottenere il QTY di un prodotto specifico da un articolo dell'ordine in Magento 1.6. Questo dovrebbe essere visualizzato in una tabella semplice. Questo è il mio codice finora:Magento: come ottenere qty di un prodotto specifico in un articolo dell'ordine?

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

Tutto è popolato correttamente e l'unica cosa che manca è il QTY. Puoi vedere l'immagine allegata per l'output.

Qualsiasi aiuto è molto apprezzato.

enter image description here

UPDATE

Quando uso:

$items = $order_data->getAllItems(); 

ho la Qty ma ora tutti gli ordini di rispettivi prodotti non è elencato.

+0

Inoltre quando echo $ product_id che var è vuoto quindi immagino che questa linea: $ product_id = $ item-> getProductId(); è sbagliato. – Ismailp

risposta

17

Hai provato i metodi alternativi per ottenere il qty ordinato?

$item->getData('qty_ordered'); 

o

$item['qty_ordered']; 
+0

Sì, la seconda soluzione ha funzionato perfettamente! Grazie! – Ismailp

+0

Nessun problema in qualsiasi momento! –

12

C'è un'altra funzione semplice per questo:

$item->getQtyOrdered(); 
Problemi correlati