Get order ids with status = 'Complete' in Magento

36,742

Solution 1

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";
}

Solution 2

To Get All the Products with order status as 'Completed'

$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
->addFieldToFilter('status', 'complete')
->setOrder('created_at', 'desc');

$this->setOrders($orders);
foreach ($orders as $order)
{
$order_id=$order->getRealOrderId();
$order = Mage::getModel('sales/order')->load($order_id, 'increment_id');
$order->getAllVisibleItems();
$orderItems = $order->getItemsCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('product_type', array('eq'=>'simple'))
    ->load();
foreach($orderItems as $Item)
{
    $Item = Mage::getModel('catalog/product')->setStoreId($Item->getStoreId())->load($Item->getProductId());
    if ($Item->getId())
    {
        echo $Item->getName();
        echo $Item->getPrice();
        echo $Item->getProductUrl();
        echo $Item->getImageUrl();
        }
    }
}
?>
Share:
36,742
ivn
Author by

ivn

Updated on July 09, 2022

Comments

  • ivn
    ivn almost 2 years

    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.

  • ivn
    ivn over 12 years
    Thanks for your reply. I tried your code and it does work but I have got three orders in the database with status as Complete. However it just showed me one of the email ids out of those 3 orders. I want to display all the orders (id n contact emails) with status as 'Complete'. Also if I need to print the order Id too, is there any method as getOrderId() or getId() or something to do that
  • Max
    Max over 12 years
    You might want to do some reading on Magento development. I've found the best articles to be Alan Storm's articles on his website: alanstorm.com/category/magento To get Id you can do $order->getId() inside of the loop. You might want to do $order->getIncrementId() though, depending on what you need. About your problem, I don't know what to say, except try removing the filter and printing all of the orders and statuses and ids like this echo $order->getId() . ": '" . $order->getStatus() . "', " . $email . "\n";
  • ivn
    ivn over 12 years
    Well I do want orders with status = complete but I also want to see Order Id, Customer Name, Customer Email for those particular orders. So your previous solution was fine just that I wanted the Order ID too. However even I tried getId() and getIncrementId() but none of them seems to be returning me any order ids.
  • ivn
    ivn over 12 years
    Also, what if I want to send an email to my customer whose order status is complete only once. i.e. that particular customer should not get the email twice, how do I achieve that. Is it easy to do it?
  • Max
    Max over 12 years
    Make an observer that fires when the order completes. alanstorm.com/magento_config explains about observers.