Filter magento order collection

Filter magento order collection

August 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Filter magento order collection

Loading Magento Order Collection: Filtering by Status

As a senior developer, I often encounter requests to filter collections based on specific criteria. In your case, you’re looking to ignore orders with status “canceled” or “complete”. Let’s dive into how you can achieve this using Magento’s collection methods.

Loading the Order Collection

First, let’s confirm the basic syntax for loading an order collection:

// Load Order Collection
$order_collection = Mage::getModel('sales/order')->getCollection();

This will give you a full collection of all orders in your system. However, it’s not yet filtered according to status.

Filtering by Status

To filter this collection based on the order status, you can use the addAttributeToFilter method:

// Load Order Collection and Filter by Status
$order_collection = Mage::getModel('sales/order')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', array('nin' => array('canceled','complete')));

Here’s what each part of the code does:

  1. ->getCollection(): This loads the order collection.
  2. ->addAttributeToSelect('*'): This selects all attributes for each order in the collection. While it might seem unnecessary, selecting all attributes ensures you can access any information about the orders later on without re-fetching data.
  3. ->addAttributeToFilter('status', array('nin' => array('canceled','complete'))): This is where the magic happens. The addAttributeToFilter method allows us to apply a condition to our collection.

The 'status' argument specifies which attribute we want to filter by—in this case, the order status. The 'nin' operator stands for “not in”, and it’s used with an array containing the statuses you want to exclude.

Why This Works

This method works because Magento uses a concept of “attributes” for its entities (like orders). These attributes allow us to apply filters based on their values.

In your case, you’re filtering out orders where the status attribute doesn’t match any of the values in the array. So, it’s effectively saying: “Give me all orders except those with status ‘canceled’ or ‘complete’.”

Conclusion

Filtering a Magento order collection is a common task that requires understanding how to apply conditions to collections using the addAttributeToFilter method.

Remember, selecting all attributes ('*') ensures you can access any information about the filtered orders without re-fetching data. This approach improves performance by reducing database queries.

For more advanced filtering techniques or if you need to filter based on multiple conditions, you might want to look into using addFieldToFilter and combining it with addAttributeToFilter.

This is a powerful feature of Magento’s collection interface that allows for flexible data manipulation. If you’re working with large datasets, proper use of filters can greatly enhance your application’s performance.

As always, feel free to reach out if you have any further questions or need additional guidance with Magento development and filtering techniques. We’re here to help!

Back to Laravel Company

Orders Pricing

Our network