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
How can we find total profit on the dashboard in Magento?
You need to develop a custom functionality for the same
Thanks for the reply.
Already we developed as per our requirement.
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.
I just browsed further on your website, and found your extension, thank you!
it working perfectly thank you
like this i want to use new filed like cost2
Can you tell me suggestion
You can make the customization in the renderer file to consider your new field cost2
How to display overall cost price and profit amount in bottom of report.
Which report are you talking about?
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.
Could you paste the snippet of your code ?, to let us understand further.
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
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 🙂
Will test it in ver 1.8, could you please tell me what issue was faced ?
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.
Maintenance flag is set from your site’s connect manager, it has nothing to do with extension, also please follow the steps as written in this post http://blog.decryptweb.com/orderprofit-magento-extension/
Thank you for this tweak. But now how can I get a profir report from a whole year?
Regards
In Magento Admin > Reports > Sales > Orders, there are various filters available, please check it
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.
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;
}
}
}
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?
have replied to your email, please revert back to me if any further issues
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
it display 0.00 in profit column can you please help me out for this ???
@gaur- Because cost price and selling price might be same for the order items
i intigrate your code but nothing to display. how to display block in xml file
@gauri – Please re-check the names of the classes, and for blocks check this post of mine Blocks in Magento eCommerce. Hope this helps you.
How would you get this working with the filters?
filters by the ID instead for me.
James
For filtering it should be part of the collection
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.
removed the HTML tags, i get the below plus/minus the open close arrows:
span class=”price”>$52.95</span
Followed the steps, added php tags to the Renderer.php page, nothing displays.
Magento 1.5.1.0
@Ric – Is a new column seen ? , can you check whether renderer file is called or not
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?
@Thijs – Which version of Magento are you using ?, can you send me the files you developed for checking if its fine with you.
Not working for me… some order just show “0.00” for profits.
Using Magento 1.6.2
@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.
It worked! Thanks!!!
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
@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
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?
@Nita – can you kindly place your code here or send to me for reference