Monday, May 7

How to add Order Profit column in Magento

here is the step by step tutorial to get order profit column in magento backend.

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.


How to get Table Name in Magento

Here is the small snippet of code to get the table name of the particular module.

let us assume my module name is "dckapbanner".

here is the script to get the table name of the "dckapbanner" module.

<?php
 
$resource = Mage::getSingleton('core/resource');
$tableName = $resource->getTableName('dckapbanner/dckapbanner');
 
?>