Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento : How to get qty of a specific product in an order item?

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:

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

Everything is populating correctly and the only thing missing is the QTY. You can see the attached image for the output.

Any help is greatly appreciated.

enter image description here

UPDATE

When I use:

$items = $order_data->getAllItems();

I get the Qty but now all the orders of respective products is not listed.

like image 791
Ismailp Avatar asked Apr 15 '13 08:04

Ismailp


People also ask

How can I get Qty in Magento 2?

Get Product qty or stock information by Product Object is pretty easy in Magento 2. We can simply get product qty from the product id and get the first product object. We can get product data from either by id or by SKU. You can fetch the Product Stock related data using the above Stock object.

How do you display order information in checkout success page in Magento 2?

You probably want to refresh your checkout/success page a lot, so to tackle that problem, go to file app/code/Magento/Checkout/Controller/Onepage/Success. php and change at line 22. // $session->clearQuote(); This way, your quote won't get cleared when you open the page.

How many products Magento can handle?

Takeaway. Magento 2 can easily handle more than 1 million products.


2 Answers

Have you tried the alternate methods to get the qty ordered?

$item->getData('qty_ordered');

or

$item['qty_ordered'];
like image 55
Joshua Schuler Avatar answered Nov 15 '22 22:11

Joshua Schuler


There is another simple function for this :

$item->getQtyOrdered();
like image 39
dr.linux Avatar answered Nov 15 '22 22:11

dr.linux