Magento 1.9 – Retrieve Last 7 Days Orders

magento-1.9sales-order

How to get last 7 days order information. I don't know how to write Collection of this,

Can you please guide some one ?

<?php
include_once "app/Mage.php";
umask(0);
Mage::app();

$fromDate = date('Y-m-d H:i:s', strtotime('-7 days'));
$todayDate = Mage::getModel('core/date')->date('Y-m-d H:i:s');



$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
foreach ($orders as $orderkey => $ordervalue) {
    echo $eid = $ordervalue->getOrderId();    
    echo $order           = Mage::getModel("sales/order")->load($eid);
    echo $customerorderid = $order->getIncrementId();
    echo $customeremailid = $order->getCustomerEmail();
    echo $customername    = $order->getCustomerFirstname();
}

?>

Best Answer

Please try this in test.php

 <?php
    include 'app/Mage.php';
    ini_set('display_errors', 1);
    Mage::app();

    date_default_timezone_set('Australia/Sydney'); // specify your time zone
    $ordershippeddays = 7; // number of days you want
    $fromDate         = gmdate("Y-m-d H:i:s", gmmktime(0, 0, 0, gmdate("m"), gmdate("d") - $ordershippeddays, gmdate("Y")));
    //echo "<br/>";
    $toDate           = gmdate("Y-m-d H:i:s", gmmktime(23, 59, 59, gmdate("m"), gmdate("d"), gmdate("Y")));

    echo "fromdate--".$fromDate; //from date
    echo "<br>";
    echo "toDate--".$toDate; // todate
    echo "<br>";
    $ordercollection = Mage::getResourceModel('sales/order_item_collection')
        ->addAttributeToSelect('order_id')
        ->addAttributeToSelect('created_at')
        ->addAttributeToFilter('created_at', array('from' => $fromDate, 'to' => $toDate))
        ->load();




    foreach ($ordercollection as $orderkey => $ordervalue) {
        $eid = $ordervalue->getOrderId();   
        $order           = Mage::getModel("sales/order")->load($eid);
        $customerorderid = $order->getIncrementId();
        $customeremailid = $order->getCustomerEmail();
        $customername    = $order->getCustomerFirstname();

        echo "orderid---".$customerorderid;
        echo "<br>";
    }

    ?>
Related Topic