Magento – How to load/get orders by period

adminordersreportssales

So I am trying to add a new column in the Orders report.

I decided to get the orders by period, so I did this $order_id = $row->getData('period');

Then I tried to load the orders using

$sales_model = Mage::getModel('sales/order')->load($order_id);
$subtotal = $sales_model->getSubtotal();
$items = $sales_model->getAllItems();

But it won't work. I tried outputting $sales_model and it's empty.

Thanks in advance!

EDIT:

From ProxiBlue (still returns nothing):

<?php
class Mage_Adminhtml_Block_Report_Sales_Grid_Column_Renderer_Profit extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
    public function render(Varien_Object $row)
    {
        $fromDate = date('Y-m-d H:i:s', strtotime($fromDate));
        $toDate = date('Y-m-d H:i:s', strtotime($toDate));

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

        return $orderCollection;

    }
}

Best Answer

Ok,

Firstly I think you are not understanding what Models and Collections are. There is no attribute, or value called period. Not sure where you got that from.

A model as in Mage::getModel('sales/order') will be an object containing the relevant data of one item. Generally that is laoded directly by the entity_id of the order that you are interested in. (->load($order_id))

So, considering the code you supplied in your question, what you have tried would not work, as you are wanting back a Collection of Models

So, to get a collection, you would use Mage::getModel('sales/order')->getCollection(), and then apply filters to that collection (in your case date ranges). You would then be able to iterate the collection, and deal with each model held in the collection in any way you want.

To answer your question:

$fromDate = date('Y-m-d H:i:s', strtotime(<YOUR START DATE>));
$toDate = date('Y-m-d H:i:s', strtotime(<YOUR END DATE>));

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

you woudl then do something with the collection.

foreach ($orderCollection as $orderModel) {
       // do somehing with the Model
}

Hope that helps.

EDIT1:

To test the query, output the raw sql, and run it in a mysql client.

$select = $orderCollection->getSelect();
echo (string) $select;
Related Topic