Magento - Check for Customer's First Order

Magento - Check for Customer's First Order

July 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Check for Customer's First Order

Checking for a Customer’s First Order in Magento

As a Magento developer, you might find yourself needing to determine if a logged-in customer has ever placed an order within your Magento store. This can be particularly useful when implementing promotional emails or targeted marketing campaigns for new customers.

Checking the Customer’s Order History

The first step is to retrieve the list of orders associated with the currently logged-in customer. The Mage_Sales_Model_Resource_Order_Collection class provides a convenient way to achieve this. Here’s how you can get started:

$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('customer_id', $customerId);

Checking for the First Order

Now that we have a collection of orders, we need to check if this is the first order placed by the customer. The simplest approach is to count the number of orders and compare it to 1:

if ($orders->count() === 1) {
    // This is the customer's first order
}

However, this method has a small issue. If the customer has no order history at all (due to an error or deletion), $orders will be null, and calling ->count() on it will result in a fatal error.

To handle this gracefully, we should first check if the collection is not empty:

if ($orders && $orders->getSize() === 1) {
    // This is the customer's first order
}

Optimizing Performance

In a high-traffic store, repeatedly querying for the first order can be resource-intensive. To optimize this, you could use Magento’s built-in caching capabilities.

You could create an observer that triggers after every new order (e.g., sales_order_place_after event), and update a flag in the customer’s metadata or session. This way, you don’t need to query for orders each time, reducing database load:

$customerId = Mage::getSingleton('customer/session')->getCustomerId();
Mage::getModel('core/session')->setFirstOrderFlag($customerId);

Using Laravel Company’s Magento Services

If your store is growing and you’re looking to optimize performance further, consider the Magento Support & Optimization Services offered by our team at Laravel Company.

Our experts can help identify bottlenecks in your order processing system and implement efficient solutions tailored to your specific needs.

Conclusion

Checking for a customer’s first order is a common requirement in Magento development. By using the built-in Mage_Sales_Model_Resource_Order_Collection class and applying some simple logic, you can easily determine if a logged-in customer has placed an order before.

For larger stores facing performance challenges, our professional Magento optimization services offer a reliable solution to ensure your store remains fast and efficient for your customers.

Performance Email Orders Customers

Our network