Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get orders items from last 24 hours in Magento

Tags:

magento

I'm attempting to get all order items from the last 24 hours. I have the query locked down, so it's giving me back what I need (an order_id, and created_on value).

$order_items = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToSelect('order_id')
    ->addAttributeToSelect('created_at')
    ->addFieldToFilter('sku', $membership_sku)
    ->toArray();

I've searched all over, and it looks like I need to add another ->addFieldToFilter() property, but I'm not quite sure how it should be structured. Any examples would be extremely helpful.

If it helps, I'm using Magento Enterprise v1.12.0.2

like image 396
Nick Parsons Avatar asked Jan 04 '13 04:01

Nick Parsons


2 Answers

After some additional research, I came across the documentation here. I decided to use strtotime since it's easier, however if someone else has a better solution, please let me know.

->addFieldToFilter('created_at', array(
    'from'     => strtotime('-1 day', time()),
    'to'       => time(),
    'datetime' => true
))
like image 80
Nick Parsons Avatar answered Sep 25 '22 00:09

Nick Parsons


I think you will need to reformat your times to be able to get good results:

$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 86400; // 60*60*24
$from = date('Y-m-d H:i:s', $lastTime);
$order_items = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToSelect('order_id')
    ->addAttributeToSelect('created_at')
    ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
    ->load();
like image 31
mpaepper Avatar answered Sep 23 '22 00:09

mpaepper