php Magento:如何在订单项目中获取特定产品的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16010323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento : How to get qty of a specific product in an order item?
提问by Ismailp
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:
我有这个脚本来帮助我从 Magento 1.6 中的订单项中获取特定产品的数量。这应该在一个简单的表格中可视化。到目前为止,这是我的代码:
// 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.
任何帮助是极大的赞赏。
UPDATE
更新
When I use:
当我使用:
$items = $order_data->getAllItems();
I get the Qty but now all the orders of respective products is not listed.
我得到了数量,但现在未列出相应产品的所有订单。
回答by Joshua Schuler
Have you tried the alternate methods to get the qty ordered?
您是否尝试过其他方法来订购数量?
$item->getData('qty_ordered');
or
或者
$item['qty_ordered'];
回答by dr.linux
There is another simple function for this :
还有另一个简单的功能:
$item->getQtyOrdered();