Get order ids with status = 'Complete' in Magento

ivn picture ivn · Nov 29, 2011 · Viewed 36.3k times · Source

I am working on getting order ids and other details for orders with status ='complete' in Magento. I am sure there is a way in magento where we can retrieve all orders with status as Complete. Since I am a new-bie to magento I am finding it hard to work this out.

I would like to send the customers with order status as Complete an email and mark them once an email is sent. But thats the later part of it. Can any one tell me how in magento can you get all order id's with status as Complete ?

Any help is appreciated. Thanks in advance.

Answer

Max picture Max · Nov 29, 2011

This can be run as a script from the base Magento install folder. If its running inside of a Magento file already (controller or block or whatever) you don't need the first three lines.

<?php
require_once('app/Mage.php');
Mage::app();

$orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('status', 'complete')
    ->addAttributeToSelect('customer_email')
    ;
foreach ($orders as $order) {
    $email = $order->getCustomerEmail();
    echo $email . "\n";
}

EDIT:

To see all orders with statuses and emails:

$orders = Mage::getModel('sales/order')->getCollection()
    //->addFieldToFilter('status', 'complete')
    ->addAttributeToSelect('customer_email')
    ->addAttributeToSelect('status')
    ;
foreach ($orders as $order) {
    $email = $order->getCustomerEmail();
    echo $order->getId() . ": '" . $order->getStatus() . "', " . $email . "\n";
}