Orders collection - how to get all in the time range (current day)
-
11-12-2020 - |
Pergunta
Trying to gather all the orders (current day) and sum totals
Updated:
$format = Mage::getModel('core/date')->date('Y/m/d');
$from = $format.' 00:00:00';
$now = Mage::getModel('core/date')->date('Y/m/d H:m:i');
$torders = Mage::getModel('sales/order')
->getCollection()
->addAttributeToSort('increment_id', 'DESC')
->addFieldToFilter('state',Array('neq'=>Mage_Sales_Model_Order::STATE_NEW))
->addAttributeToFilter('created_at', array('from' => $from,'to' => $now))
;
everything is fine except one little thing, one of the orders seems to be not in collection.
Checked all customer_emails
and the order created just before midnight (00:14:46) is not included in collection for some reason.
suppose to be 127 order while getSize()
is saying 126 regardless what filters used etc.
Any ideas ?
Solução
Try this
$from = new DateTime('2017-08-29 00:00:00');
$from = $from->format('Y-m-d H:i:s');
$to = new DateTime('2017-08-29 23:59:9');
$to = $to->format('Y-m-d H:i:s');
$orders = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('created_at', array('from'=>$from, 'to'=>$to))
->addAttributeToFilter('status', array('neq' => Mage_Sales_Model_Order::STATE_NEW));
->addAttributeToSelect('*');
Outras dicas
Hi make sure pass proper time as well in from and to check below Idea.
$format = 'Y-m-d';
$from = date($format)." 00:00:00";
$to = date($format)." 23:59:59";;
Try this code:
<?php
$from_date = new DateTime('2017-08-17 00:00:00');
$from_date = $from_date ->format('Y-m-d H:i:s');
$to_date = new DateTime('2017-08-29 23:59:9');
$to_date = $to_date->format('Y-m-d H:i:s');
$orders = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('created_at', array('from'=>$from_date, 'to'=>$to_date))
->addAttributeToFilter('status', array('neq' => Mage_Sales_Model_Order::STATE_COMPLETE));
echo '<pre>'; print_r($orders->getData());
?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange