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.

Updates [September 19,2013] : I have developed an extension for this, now no need to do it manually, please browse to OrderProfit Magento Extension

42 thoughts on “Order Profit column in Magento

  1. Hi, does this code still work in 1.9.2? i am quite new to magento, and would like to add this to the array of tools i have available.

    1. I just browsed further on your website, and found your extension, thank you!

  2. it working perfectly thank you

    like this i want to use new filed like cost2

    Can you tell me suggestion

    1. Order Sales Report. I have created module for order sales report with product cost price. I can’t display overall cost price for all order.

  3. Hello,

    Working fine on magento 1.7 thanks for the code.

    The only problem when exporting to csv, there is span class=”price”> in profit colomn.

    But everything else works just fine

    Thanks

  4. Thanks for the tutorial. This does not seem to work with 1.8. Do you have any plans to make it work with 1.8? I would love to see this 🙂

    1. Great! When using the tutorial version, the column did not show up at all for me. When using your extension, I could not install through Connect Manager and when I installed manually, it placed the maintenance flag in the root and I got the service temporarily out of service error.

  5. Thank you for this tweak. But now how can I get a profir report from a whole year?
    Regards

  6. Great tutorial. Helped me a lot!!! I changed a small bit due to tax reasons.

    Now I am looking into the profit calculation when a item is refunded. It does not update the amount because it is not in the function.

    I was thinking along the lines of:

    if(!empty($items))
    {
    foreach ($items as $itemId => $item)
    {
    $qty = (intval($item->getQtyOrdered())-intval($item->getQtyOrdered())); //items shipped and not refunded.
    if(empty($qty))
    {
    $qty = 1;
    }
    $b_cost = $item->getBaseCost();//get item cost
    $base_cost[] = ($b_cost*$qty); //get all items cost
    }
    }

    Not sure if this is the right path. Next I would have to also substract total_refunded from the total paid:

    $subtotal = $sales_model->getTotalInvoiced() – $sales_model->getTotalRefunded()

    Will let you know how this develops.

    1. My final (for now) script: (keep in mind I divide for 21% VAT)

      getData($this->getColumn()->getIndex());

      if(!empty($order_id))
      {
      $sales_model = Mage::getModel(‘sales/order’)->load($order_id);
      $Invoiced = $sales_model->getTotalInvoiced();//get order subtotal (without shipping)
      $Refunded = $sales_model->getTotalRefunded();//total amount refunded
      $items = $sales_model->getAllItems(); //get all order items
      $base_cost = array();
      $canceled = $sales_model->isCanceled();
      $subtotal = $Invoiced-$Refunded;
      if(!empty($items))
      {
      foreach ($items as $itemId => $item)
      {
      $qtyBought = intval($item->getQtyOrdered()); //get items quantity
      $qtyRefunded = intval($item->getQtyRefunded());
      if(empty($qtyBought))
      {
      $qtyBought = 1;
      }
      $b_cost = $item->getBaseCost();//get item cost
      $base_cost[] = ($b_cost*($qtyBought-$qtyRefunded)); //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/1.21)-$total_order_cost); //get profit , subtraction of order subtotal.
      }
      if(!empty($canceled))
      {
      $profit=0;
      }
      $_coreHelper = $this->helper(‘core’);
      $profit = $_coreHelper->currency($profit);

      return $profit;
      }

      }
      }

  7. I have implemented this exactly and everything displays fine but the profit still shows as 0. I have checked that the cost is entered as well as the price. Anything else I should look for?

  8. Hi,

    working fine on magento 1.7 thx for the code.

    The only problem when exporting to csv, there is span class=”price”> in profit colomn.

    But everything else work just fine

    Thanks

  9. How would you get this working with the filters?

    filters by the ID instead for me.

    James

  10. When i try to export to CSV, i get the following in the cells for the ‘Profit’ column:
    $52.95

    Anyone know how i could remove the HTML?

    Working great for 1.6.2

    @Pablo Montenegro
    make sure you have a cost entered for the products in that order. remember that even if you add the cost after the order is placed, it will still show ‘0’ because it’s grabbing the base cost from the DB.

  11. Followed the steps, added php tags to the Renderer.php page, nothing displays.

    Magento 1.5.1.0

  12. Tried the code, but it gives me a parse error on “public function render(Varien_Object $row)” in Profit.php:

    Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION

    Any ideas?

    1. @Thijs – Which version of Magento are you using ?, can you send me the files you developed for checking if its fine with you.

  13. Not working for me… some order just show “0.00” for profits.
    Using Magento 1.6.2

    1. @Pablo – Please calculate the order profit (i.e, the difference between Cost and Selling Price) manually for testing and let me know if it gives value greater than zero.

  14. 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

    1. @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

  15. 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

Your email address will not be published. Required fields are marked *

Scroll to top