我正在处理一个应该在每晚结束时运行的脚本。它应该查看当天销售了多少特定产品(最好是SKU)并计算出来。这个过程还有第二部分,但是现在我满足于回应正确的值。我对如何做到这一点感到困惑。以下是我到目前为止的情况:
require_once('../app/Mage.php');
ini_set("error_reporting",E_ALL);
ini_set("display_errors",true);
umask(0);
Mage::app('admin');
// Some sort of SKU counter variable
$sku_counter = 0;
// Create order collection object
$collection = Mage::getModel('sales/order')->getCollection();
// Apply date filter. This would be 1 day in production but using this range as a test.
$collection->addFieldToFilter(
'created_at',
array('from' => '2012-05-01', 'to' => '2012-05-09')
);
// Iterate it for displaying results
foreach ($collection as $order) {
// This is where I fall apart. I know I need to either get all items or only get
// items with the specific SKU. After that I need to get the quantity sold of that SKU
// and add it to my $sku_counter.
echo 'There were " . $sku_counter . "sales of SKU XX-XXXX yesterday.";
}
感谢任何帮助!
答案 0 :(得分:2)
试试这个:
require_once('../app/Mage.php');
ini_set("error_reporting",E_ALL);
ini_set("display_errors",true);
umask(0);
Mage::app('admin');
// Some sort of SKU counter variable
$sku_counter = array();
// Create order collection object
$collection = Mage::getModel('sales/order')->getCollection();
// Apply date filter. This would be 1 day in production but using this range as a test.
$collection->addFieldToFilter(
'created_at',
array('from' => '2012-05-01', 'to' => '2012-05-09')
);
// Iterate it for displaying results
foreach ($collection as $order) {
foreach ($order->getAllItems() as $item) {
if (!isset($sku_counter[$item->getSku()])) {
$sku_counter[$item->getSku()] = 0;
}
$sku_counter[$item->getSku()] += (float) $item->getQtyOrdered();
}
}