2

我正在编写一个应该在每晚结束时运行的脚本。它应该查看当天销售了多少特定产品(最好按 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.";

}

任何帮助表示赞赏!

4

1 回答 1

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();
    }
}
于 2012-05-10T14:29:55.290 回答