Nick Parsons
Nick Parsons

Reputation: 8607

Get orders items from last 24 hours in 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

Upvotes: 10

Views: 20964

Answers (2)

mpaepper
mpaepper

Reputation: 4022

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();

Upvotes: 20

Nick Parsons
Nick Parsons

Reputation: 8607

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
))

Upvotes: 15

Related Questions