Get orders items from last 24 hours in Magento

Get orders items from last 24 hours in Magento

September 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get orders items from last 24 hours in Magento

Retrieving Recent Order Items in Magento

As a senior developer working extensively with e-commerce platforms like Magento, I often receive queries from fellow developers seeking guidance on more specific tasks within the system.

One such common requirement is to fetch all order items created within a specific time frame, usually the last 24 hours. Let’s break down how to accomplish this using Magento’s built-in resources and collections effectively.

Understanding Order Item Collections in Magento

Magento provides a robust collection model for managing and filtering order items. The sales/order_item_collection is a powerful resource that allows you to retrieve order items based on various attributes and filters.

In your provided code, you have already successfully selected the necessary attributes (order_id and created_at) and filtered orders by a specific SKU. This is an excellent starting point.

Adding Time Filter for Recent Items

To filter order items by their creation time, you can use the created_at attribute along with the addFieldToFilter() method. Here’s how to structure your query correctly:

$order_items = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToSelect('order_id')
    ->addAttributeToSelect('created_at')
    ->addFieldToFilter('sku', $membership_sku)
    ->addFieldToFilter('created_at', array(
        'from' => date('Y-m-d H:i:s', strtotime('-24 hours')),
        'to'   => now()
    ))
    ->load();

In this example, we’ve added another field filter for the created_at attribute. We specify an array with from and to keys to define the time range.

The from value is calculated using PHP’s strtotime() function, which subtracts 24 hours from the current date and time. The to value is set to the current timestamp using the now() function.

Finally, we use the load() method to execute the query and return the results as an array of order items.

Conclusion

Retrieving recent order items in Magento involves leveraging the platform’s built-in collections and filters effectively. By understanding how to structure your queries using the addFieldToFilter() method with appropriate attribute values, you can extract the required data efficiently.

Remember that working with large datasets may impact performance, so it’s essential to optimize your queries and consider implementing pagination or limiting results where necessary.

For more advanced techniques in Magento development and optimization, I recommend exploring our extensive guides on Magento performance tuning at LaravelCompany.

Database Orders

Our network