2012-05-10 119 views
2

我正在研究應該在每晚結束時運行的腳本。它應該查詢當天銷售了多少具體產品(最好由SKU)並將其歸類。這個過程還有第二部分,但現在我只需要回應正確的價值即可。我對我應該如何做到這一點感到困惑。這是我到目前爲止:從Magento銷售/訂單模型中提取項目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."; 

} 

任何幫助表示讚賞!

回答

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(); 
    } 
} 
+0

謝謝。會給它一個鏡頭並報告回來! – jkphl

相關問題