无法获得季度销售

时间:2012-06-26 20:04:07

标签: php magento

我正试图提取第一季度的销售总额。我似乎无法让它发挥作用。有什么建议吗?

$total = 0;
$orders = Mage::getModel('sales_order/collection')
   ->addAttributeToSelect('*')
   ->addAttributeToFilter('created_at', array(
      'from' => '2012-01-01',
      'to' => '2012-03-31'));
foreach ($orders as $order) {
   $total += $order->getGrandTotal();
}
echo $total;

1 个答案:

答案 0 :(得分:1)

您没有正确收集该集合。有多种方法可以做到这一点,看起来你可能会结合几种方法:

  1. Mage::getModel('sales/order')->getCollection()
  2. Mage::getResourceModel('sales/order_collection')
  3. 但是,如果您真正想要的是将单个属性grand_total相加,则方式更有效地构建您自己的查询,而不是加载整个销售订单集合:

    $db = Mage::getSingleton('core/resource')->getConnection('core_read');
    $salesTable = Mage::getSingleton('core/resource')->getTableName('sales/order');
    list($total) = $db->fetchCol(
        $db->select()
           ->from($salesTable, array('grand_total' => 'SUM(grand_total)'))
           ->where('DATE(created_at) >= ?', '2012-01-01')
           ->where('DATE(created_at) <= ?', '2012-03-31')
    );
    echo $total;