search
top

Order Profit column in Magento

I am explaining here the way to add a Profit column in Order grid seen in admin-end

This column will show the profit gained per order (i.e, the difference between Cost and Selling Price)

1) Copy the app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php file to app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php, by maintaining the directory structure

2) There is a protected function _prepareColumns, kindly paste the below code inside it

		//below code for showing profit
        //start
         $this->addColumn('entity_id', array(
            'header' => Mage::helper('sales')->__('Profit'),
            'index' => 'entity_id',
            'type'  => 'currency',
            'currency' => 'order_currency_code',
            'renderer'  => new Mage_Adminhtml_Block_Sales_Order_Renderer_Profit() //for the value
        ));
        //end

3) Create a file app/code/local/Mage/Adminhtml/Block/Sales/Order/Renderer/Profit.php (directory structure should be maintained).
Copy the following code as it is in it.


class Mage_Adminhtml_Block_Sales_Order_Renderer_Profit extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
    	$order_id = $row->getData($this->getColumn()->getIndex());

		if(!empty($order_id))
		{
			$sales_model = Mage::getModel('sales/order')->load($order_id);
			$subtotal = $sales_model->getSubtotal();//get order subtotal (without shipping)
			$items = $sales_model->getAllItems(); //get all order items
        	$base_cost = array();
        	if(!empty($items))
        	{
				foreach ($items as $itemId => $item)
				{
					$qty = intval($item->getQtyOrdered()); //get items quantity
					if(empty($qty))
					{
						$qty = 1;
					}
					$b_cost = $item->getBaseCost();//get item cost
					$base_cost[] = ($b_cost*$qty); //get all items cost
				}
        	}
        	$total_order_cost = '';
        	if(!empty($base_cost))
        	{
        		$total_order_cost = array_sum($base_cost); //get sum of all items cost
        	}
        	$profit = '';
        	if(!empty($total_order_cost))
        	{
        		$profit = ($subtotal-$total_order_cost); //get profit , subtraction of order subtotal
        	}

        	$_coreHelper = $this->helper('core');
			$profit = $_coreHelper->currency($profit);

			return $profit;
		}

    }
}

And you are done, now a Profit column will be seen in Sales > Orders in adminend

Hope this is helpful.



4 Responses to “Order Profit column in Magento”

  1. jay says:

    i added this and nothing appears.

    using magento 1.6 community

    this file i need to create right with the code given inside php tags

    app/code/local/Mage/Adminhtml/Block/Sales/Order/Renderer/Profit.php

    • DWRoot says:

      @Jay – Yes the code should be placed inside PHP tags, please confirm that the file name ‘Profit’ (or whatever you named it) and the path should match with ‘Mage_Adminhtml_Block_Sales_Order_Renderer_Profit()’ (here if the file is names Abc it should be same in the class name), hope this resolves your issue, let me know if still problem persists

  2. nita says:

    I’ve modified your code a bit to display the total cost of the order item but some items are showing up as 0.00. any ideas?

Leave a Reply

top