2013-04-15 24 views
6

Mam ten skrypt, aby pomóc mi uzyskać QTY określonego produktu z elementu zamówienia w Magento 1.6. Powinno to zostać zwizualizowane w prostej tabeli. To jest mój kod do tej pory:Magento: Jak uzyskać ilość konkretnego produktu w zamówieniu?

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

Wszystko wypełniania poprawnie i jedyną rzeczą, brakuje jest st. Możesz zobaczyć załączony obraz dla wyjścia.

Każda pomoc jest bardzo doceniana.

enter image description here

UPDATE

Gdy używam:

$items = $order_data->getAllItems(); 

uzyskać Ilość ale teraz wszystkie rzędy odpowiednich produktów nie ma na liście.

+0

Również kiedy echo $ PRODUCT_ID że zmienna jest pusta, więc myślę, że ta linia: $ product_id = $ item-> getProductId(); jest źle. – Ismailp

Odpowiedz

17

Czy próbowałeś alternatywnych metod, aby uzyskać zamówione qty?

$item->getData('qty_ordered'); 

lub

$item['qty_ordered']; 
+0

Tak, drugie rozwiązanie działało idealnie! Dzięki! – Ismailp

+0

Nie ma problemu w każdej chwili! –

12

Jest inna prosta funkcja to:

$item->getQtyOrdered(); 
Powiązane problemy