Coleção Magento Filter por horários de criação (hoje, ontem, semana, hora, etc.)

9

Tenho uma coleção personalizada que desejo filtrar por data de criação e entradas het criadas "ontem"

Entradas da coleção

//dates are set in controller using
setCreatedTime(Mage::getModel('core/date')->gmtDate()); 

Criado ontem (não funciona)

//3 products items Yesterday
//below filtering outputs incorrect entries
$collection = Mage::getModel('things/things')->getCollection();

Eu tentei, mas gera entradas incorretas;

//thought strtotime('yesterday') would work..
$collection->addFieldToFilter('created_time', array('gt' => Mage::getModel('core/date')->date('Y-m-d H:i:s', strtotime('yesterday'))));
$collection->addFieldToFilter('created_time', array('gt' => Mage::getModel('core/date')->date('Y-m-d H:i:s', strtotime('-1 day'))));
$collection->addFieldToFilter('created_time', array('from'=> strtotime('-1 day', time()),'to'=> time(),'datetime' => true));
$fromDate = date('Y-m-d H:i:s', strtotime($fromDate));
$toDate = date('Y-m-d H:i:s', strtotime($toDate));
$collection->addFieldToFilter('created_time', array('from'=>$fromDate, 'to'=>$toDate));

Criado hoje (dia atual) (obras)

//5 products items today with timestamp 2016-05-01 05:22:53
//below filtering outputs correct entries
$collection = Mage::getModel('things/things')->getCollection();
$collection->addFieldToFilter('created_time', array('gt' => Mage::getModel('core/date')->date('Y-m-d H:i:s', strtotime('today'))));

Criado na semana passada (obras)

//23 products items with timestamps for this week
//below filtering outputs correct entries
$collection = Mage::getModel('things/things')->getCollection();
$collection->addFieldToFilter('created_time', array('gt' => Mage::getModel('core/date')->date('Y-m-d H:i:s', strtotime('-1 week'))));
BENN1TH
fonte

Respostas:

10

Para adicionar à resposta @Ashvin ..

Recebi entradas criadas na última hora

$things = Mage::getModel('things/things')->getCollection();
$things->addFieldToFilter('things_type', 'view');
$fromDate = date('Y-m-d H:i:s', strtotime('-1 hour'));
$toDate = date('Y-m-d H:i:s', strtotime(now()));
$things->addFieldToFilter('created_time', array(
    'from' => $fromDate,
    'to' => $toDate,
    'date' => true,
    ));
return count($things);

e como eu fui criado ontem;

$now = Mage::getModel('core/date')->timestamp(time());
$dateStart = date('Y-m-d' . ' 00:00:00', $now);
$dateEnd = date('Y-m-d' . ' 23:59:59', $now);
$things = Mage::getModel('things/things')->getCollection();
$things->addFieldToFilter('things_type', 'view');
$things->addFieldToFilter('created_time', array('from' => $dateStart, 'to' => $dateEnd));
return count($things);
BENN1TH
fonte
5

Como resolvemos isso? simples. limitando a quantidade de pedidos apresentados na grade de pedidos nas últimas 24 horas, a menos que solicitado de outra forma.

Exemplo: - Copie o arquivo app / code / core / Mage / Adminhtml / Block / Sales / Order / Grid.php para:

app / code / local / Mage / Adminhtml / Block / Sales / Order / Grid.php

Edite a seguinte função, copie e cole a partir daqui:

protected function _prepareCollection()    {

$collection = Mage::getResourceModel($this->_getCollectionClass());

######################## FILTER BY LAST DAY ######################
$now = Mage::getModel('core/date')->timestamp(time());
$filter   = $this->getParam($this->getVarNameFilter(), null); //important - check for other requested grid-filters before filtering today's orders

$dateStart = date('Y-m-d' . ' 00:00:00', $now);
$dateEnd = date('Y-m-d' . ' 23:59:59', $now);
$postData = Mage::app()->getRequest()->getPost();
if (empty($filter)) {
$collection->addFieldToFilter('`main_table`.created_at', array('from' => $dateStart, 'to' => $dateEnd));
}
##################################################################



$collection->getSelect()->group('entity_id');
$this->setCollection($collection);

return $this;

}

use mais código para sua pergunta ... (hoje, ontem, semana, hora etc)

Ashvin Monpara
fonte
go0d funciona @ashvin
Amit Bera
Ei! Ótima solução! Como posso alterá-lo para receber apenas os pedidos de duas horas atrás?
Vladimir Despotovic