Displaying Product Cost in Magento Admin Order View
Hello developers! Today’s post addresses a common question I often encounter - how to show the product cost directly in the order view within your Magento admin panel. By default, this information is not readily available in the backend, but with some customization, we can achieve this.
Why Display Product Cost in Admin?
Displaying the cost price of each item in an order allows you to:
- Quickly identify and analyze profit margins for specific orders.
- Verify that prices charged match the actual production costs.
- Easily spot any pricing discrepancies or errors.
Method 1: Override the Admin Order View Template
The most straightforward approach is to override the adminhtml template. Here’s a step-by-step guide:
-
Navigate to
app/design/adminhtml/default/default/template/sales/order/view/items.phtml. -
Copy this file and save it as
items_override.phtmlin your custom theme folder (e.g.,app/design/adminhtml/default/mytheme/template/sales/order/view/). -
Open the new
items_override.phtmlfile for editing. -
Locate the following line of code:
<strong><?php echo $this->__('Price') ?>:</strong> <?php echo $this->getFormattedPrice($item->getPrice()) ?>
- Add the following after this line to display the product cost:
<br />
<strong><?php echo $this->__('Cost Price') ?>:</strong> <?php echo $this->getFormattedPrice($item->getData('cost_price')) ?>
-
Save the file.
-
Clear your Magento cache for the changes to take effect.
Method 2: Add a Custom Column in Admin Grid
If you prefer to display the cost price directly in the order grid, you can add a new column:
-
Create an
etc/adminhtml.xmlfile under your module directory (e.g.,app/code/local/YourCompany/YourModule/etc/adminhtml.xml). -
Add the following content to define a new column:
<adminhtml>
<layout>
<updates>
<sales_order_view>
<file>items.phtml</file>
<action>[NAMESPACE]_[MODULE]/order_grid.php</action>
</sales_order_view>
</updates>
</layout>
</adminhtml>
Replace [NAMESPACE] and [MODULE] with the appropriate values for your custom module.
- Create a new
app/code/local/YourCompany/YourModule/controllers/Adminhtml/Order/GridController.phpfile:
class YourCompany_YourModule_Adminhtml_Order_GridController extends Mage_Adminhtml_Block_Sales_Order_GridController
{
public function indexAction()
{
$this->_title($this->__('Sales'))->_title($this->__('Orders'));
$this->loadLayout();
$this->_setActiveMenu('sales');
$this->_addContent($this->getLayout()->createBlock('yourcompany_yourmodule/adminhtml_order_grid'));
$this->renderLayout();
}
}
- Create a new
app/code/local/YourCompany/YourModule/block/Adminhtml/Order/Grid.phpfile to define the grid block:
class YourCompany_YourModule_Block_Adminhtml_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('cost_price', array(
'header' => Mage::helper('sales')->__('Cost Price'),
'index' => 'cost_price',
'type' => 'number'
));
return parent::_prepareColumns();
}
}
Conclusion
Displaying the product cost in your Magento admin panel provides valuable insights for managing and analyzing orders. By following one of these methods, you can easily incorporate this information into either the order view or the order grid.
Remember to clear your cache after making any template or layout updates. Also, ensure that the cost_price attribute is added to your products if it doesn’t already exist.
If you require further assistance with Magento customizations or need help with other e-commerce development tasks, feel free to reach out to us at Laravel Company. We specialize in crafting efficient and secure solutions for businesses of all sizes.