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.

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

39 Responses to “Order Profit column in Magento”

  1. jeroen says:

    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.

  2. yamini says:

    it working perfectly thank you

    like this i want to use new filed like cost2

    Can you tell me suggestion

  3. Rifan says:

    How to display overall cost price and profit amount in bottom of report.

  4. Mug Souvenir says:

    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

  5. Cheung says:

    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 :)

  6. Pablo Montenegro says:

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

  7. Jacco says:

    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.

    • Jacco says:

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

      }
      }

  8. 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?

  9. david says:

    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

  10. gauri says:

    it display 0.00 in profit column can you please help me out for this ???

  11. gauri says:

    i intigrate your code but nothing to display. how to display block in xml file

  12. James says:

    How would you get this working with the filters?

    filters by the ID instead for me.

    James

  13. joeymetro says:

    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.

  14. Ric says:

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

    Magento 1.5.1.0

  15. Thijs says:

    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?

  16. Pablo Montenegro says:

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

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

    • Pragnesh 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

  18. 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 to yamini

top