Magento Filter orders by state

Magento Filter orders by state

October 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Filter orders by state

Filtering Magento Orders by State: A Comprehensive Guide

Introduction

As a Magento developer or store owner, you might often need to filter orders based on their current status for administrative purposes or custom processing. One common requirement is to retrieve only the “new” orders that haven’t been processed yet. In this post, we’ll guide you through the process of filtering Magento orders by state using the native API.

Understanding Magento Order Status

Magento uses a predefined set of order statuses such as ‘pending’, ‘processing’, ‘complete’, and ‘canceled’. The ‘new’ status indicates that an order has been placed but hasn’t been manually processed by the admin yet. Understanding these statuses is crucial for filtering your orders correctly.

Retrieving All Order IDs

First, let’s retrieve all the available order IDs using Magento’s built-in functionality:

$myOrder = Mage::getModel('sales/order');
$orders = Mage::getModel('sales/mysql4_order_collection');
$allIds = $orders->getAllIds();

The $allIds variable now contains an array of all order IDs in your store.

Filtering Orders by State

To filter orders by state, you can add a condition to the collection using the addFieldToFilter() method. However, there’s a common mistake made when trying to filter for the ‘new’ status:

$orders->addFieldToFilter('state', Array('eq' => "new"));

Or

$myOrder->setData('state','new');

The issue lies in the terminology. In Magento, the field name isn’t ‘state’ but rather ‘status’. Let’s correct this:

$orders->addFieldToFilter('status', Array('eq' => "pending"));

Understanding Pending Status

Note that we’re using ‘pending’ instead of ‘new’. This is because in Magento, an order immediately moves to the ‘processing’ state when it’s placed. The ‘pending’ status represents orders that haven’t been manually processed by the admin yet.

If you want to include both ‘new’ and ‘pending’ orders (which are essentially the same as far as manual processing goes), you can modify the filter like this:

$orders->addFieldToFilter('status', Array('in' => array('new', 'pending')));

Conclusion

Filtering Magento orders by state is a fundamental task in managing your e-commerce store. Understanding the correct field names and statuses is key to writing efficient queries.

Remember, you can always refer to the official Magento documentation for further details on collections and filters: https://laravelcompany.com/magento-2-collections-and-filters/

As your business grows, consider exploring advanced filtering techniques or custom database queries to optimize performance. The knowledge you’ve gained in this post provides a solid foundation for building more complex order management systems.

Happy filtering!

Database API Admin Orders

Our network