1

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();
}

?>
1
  • to use standard php date functions in magento is bad practice.
    – Anthony
    Sep 17, 2018 at 13:35

3 Answers 3

5

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

    ?>
1
  • check my updated question it not print anything
    – Magento 2
    Jan 20, 2017 at 9:28
2

Try this

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


$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate))

Edit:

foreach ($orders as $eachorder) {
    echo $eid =  $eachorder->getId();    
    echo $order           = Mage::getModel("sales/order")->load($eid);
    echo $customerorderid = $order->getIncrementId();
    echo $customeremailid = $order->getCustomerEmail();
    echo $customername    = $order->getCustomerFirstname();
}
2
  • 1
    better use date('Y-m-d H:i:s', strtotime('-7 days')) Jan 20, 2017 at 9:15
  • check my updated question it not print anything
    – Magento 2
    Jan 20, 2017 at 9:28
2

You can get order of any date range from below code.

require_once ('app/Mage.php');
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);



 $toDate = Mage::getModel('core/date')->date('Y-m-d H:i:s');


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



/* Get the collection */
$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));


foreach ($orders as $ordervalue) {
    echo $ordervalue->getId();

}
4
  • How it filter last 7 days
    – Magento 2
    Jan 20, 2017 at 9:08
  • @Kothari i have update answer
    – Kul
    Jan 20, 2017 at 9:21
  • check my updated question it not print anything
    – Magento 2
    Jan 20, 2017 at 9:28
  • @Kothari Please check updated answer
    – Kul
    Jan 20, 2017 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.