Add custom column to Magento report and sales dashboard - magento

I've been searching for a module or a way to deal with the fallowing situation:
A customer orders a product that is 100$ and pays shipping of 10$. That would charge him a total amount of 110$. By the time the product arrives he notices that the product is a little scratched and instead of sending it back he accepts to receive a discount.
For this to happen properly I would make a credit memo with an adjustment refund of let's say 30$.
I need to see the total amount that remains after this operation is done (80$) in a separate column in either reports or the sales dashboard.
For this particular task we have also installed a module called "Advanced Orders Manager by Iksanika" but this appears to only get the data that already exists in the database and is not allowing us to use variable for let's say a substraction.
Also in Magento reports we use Reports > Sales > Orders but that gives us only the total figures and we cannot find anywhere a "total amount charged" that would give us the exact final figure (80$).
This is a particular request of the accounting dpt of an online store.

What you want to do is to add a custom grid column to the sales order grid.
Magento uses the database table sales_flat_order to fill the values in the sales order grid. Unfortunately the sales_flat_order table doesn't provide the information you need (grandtotal minus refunded amount) but both values separately (grand_total and total_refunded). Because of that you have multiple options:
Extend the table sales_flat_order.
Add a custom renderer for your custom sales order grid column.
As everything in this world both methods have advantages and disadvantages.
If you extend the sales_flat_order table you have to make sure the value of your new database column is set when creating new orders. On the other hand, as the value is persisted in your database, you can use the column for other extensions.
With a custom renderer you don't have to care about persistence. You have the opportunity to do operations and return your result which will be displayed in your custom sales order grid column. As we have both grand_total and total_refunded saved already you can return the grandtotal minus refunded amount.
I'll describe how to add a custom sales order grid column and add a custom renderer to return the value of grandtotal minus refund amount.
Part 1: How to add custom column to sales order grid in backend?
To add a custom column to sales order grid first add your own sales order grid block XX_ModuleName_Block_Adminhtml_Order_Grid.
Rewrite magentos *Mage_Adminhtml_Block_Sales_Order_Grid (app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php) and extend it with your sales order grid.
To add your custom column override _prepareColumns() method. Inside the overwritten _prepareColumns() you want to first add the column from parent class. Finally, add your custom column:
note: you can download the example module.
$this->addColumn('my_column', array(
'header' => Mage::helper('sales')->__('My Column'),
'index' => 'my_column',
'type' => 'text',
'renderer' => 'XX_ModuleName_Block_Adminhtml_Order_Grid'
));
Example etc/config.xml:
<?xml version="1.0"?>
<config>
<modules>
<EG_AdminSalesOrder>
<version>1.0.0</version>
</EG_AdminSalesOrder>
</modules>
<global>
<blocks>
<eg_adminsalesorder>
<class>EG_AdminSalesOrder_Block</class>
</eg_adminsalesorder>
<adminhtml>
<rewrite>
<sales_order_grid>EG_AdminSalesOrder_Block_Adminhtml_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<eg_adminsalesorder>
<class>EG_AdminSalesOrder_Helper</class>
</eg_adminsalesorder>
</helpers>
</global>
</config>
Example custom sales order grid block:
class EG_AdminSalesOrder_Block_Adminhtml_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
/**
* Add custom column to sales order grid
*
* #return Mage_Adminhtml_Block_Widget_Grid
* #throws Exception
*/
protected function _prepareColumns()
{
$this->addColumn('real_order_id', array(
'header'=> Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('store_id', array(
'header' => Mage::helper('sales')->__('Purchased From (Store)'),
'index' => 'store_id',
'type' => 'store',
'store_view'=> true,
'display_deleted' => true,
));
}
$this->addColumn('created_at', array(
'header' => Mage::helper('sales')->__('Purchased On'),
'index' => 'created_at',
'type' => 'datetime',
'width' => '100px',
));
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
));
$this->addColumn('shipping_name', array(
'header' => Mage::helper('sales')->__('Ship to Name'),
'index' => 'shipping_name',
));
$this->addColumn('base_grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Base)'),
'index' => 'base_grand_total',
'type' => 'currency',
'currency' => 'base_currency_code',
));
$this->addColumn('grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
'index' => 'grand_total',
'type' => 'currency',
'currency' => 'order_currency_code',
));
$this->addColumn('refunded', array(
'header' => Mage::helper('sales')->__('Total - Refund'),
'index' => 'refunded',
'type' => 'text',
'renderer' => 'EG_AdminSalesOrder_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_Refunded'
));
parent::_prepareColumns();
}
}
Part 2: How to add a custom renderer for my custom column?
Now you can add your custom renderer to fill the values into your custom sales order grid column.
First add a cusom renderer class XX_ModuleName_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_MyColumn.
Then extend Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract.
Override the render(Varien_Object $row) method. Here you can do your specific operation and return the string that should be displayed in your grid. In your case you want to load the order collection for the current $row param, get the total refunded amount, subtract the grandtotal with the refunded amount and return the value.
Example custom renderer block:
class EG_AdminSalesOrder_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_Refunded
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
/**
* #param Varien_Object $row
* #return string
*/
public function render(Varien_Object $row)
{
$currentOrderId = $row->getId();
$currentOrderGrandTotal = $row->getGrandTotal();
$orderCollection = Mage::getModel('sales/order')->getCollection();
$orderCollection->addFieldToSelect('total_refunded');
$orderCollection->addFieldToFilter('entity_id', array('eq' => $currentOrderId));
$orderCollectionItem = $orderCollection->getFirstItem();
$refundedAmount = $orderCollectionItem->getTotalRefunded();
$grandTotalWithoutRefundedAmount = (float)$currentOrderGrandTotal - (float)$refundedAmount;
$grandTotalWithoutRefundedAmount = Mage::helper('core')->currency($grandTotalWithoutRefundedAmount);
return (string)$grandTotalWithoutRefundedAmount;
}
}
You can download the example module.

Related

magento show content according to condition in admin grid

I have developed custom admin module. I have used the usual methods _prepareCollection and _prepareColumns to show the data in Grid.
protected function _prepareCollection()
{
$collection = Mage::getModel("wallets/sellerrequest")->getCollection();
$collection->getSelect()
->join( array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.seller_id and ce1.attribute_id = "5"', array('seller_name' => 'value'));
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
protected function _prepareColumns()
{
$helper = Mage::helper('sellers');
$currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
$this->addColumn('id', array(
'header' => $helper->__('Request No'),
'index' => 'id'
));
$this->addColumn('Requested Amount', array(
'header' => $helper->__('Requested Amount'),
'index' => 'request_amount'
));
$this->addColumn('Seller Name', array(
'header' => $helper->__('Seller Name'),
'index' => 'seller_name',
));
$this->addColumn('Status', array(
'header' => $helper->__('Status'),
'index' => 'status_flag'
));
All the data shows correctly according to the table values. But I want to show the Request Amount column values preceding with $ sign, e.g. $300 etc. Also, I want to show the status flag according to condition. Means if the status flag is 1 then I want to show the value as "Approved", if flag is 2 then "Pending" etc. How should I customize the collection data and show in grid according to my requirement? Help appreciated.
Thanks.
I have answered to a question similar to your requirement
How to properly add a shipping_description column in magento order grid?
Check my answer and try to compare with your problem. In this example there is the solution for our currency problem too.
So check this out.Hope it will be helpful.
Here you should implement Grid Renderer.
Here is complete tutorial for that : http://inchoo.net/magento/how-to-add-custom-renderer-for-a-custom-column-in-magento-grid/
You can customize the value of any colum

Add and show new column in sales_flat_order and show this at order grid in magento

I have added a new column (exported)in sales_flat_order and add at files at this location:
app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass())
->addAttributeToSelect('*')
->joinAttribute('exported','sales/order','sales_flat_order.entity_id',null,'left');
}
protected function _prepareColumns()
{
$this->addColumn('exported', array(
'header' => Mage::helper('sales')->__('Exported'),
'index' => 'exported',
'type' => 'checkbox',
'name' =>'exported',
'value' =>$this->getExported()==1 ? 'true' : 'false',
));
}
after that it showing on order grid in admin site,but it is not showing value and name,
I am new in magento,so please help me ,
stuck from 2 days.
Thanks for Assistance.
The method _prepareCollection() uses sales_flat_order_grid table as asource, thus you have to add the column to sales_flat_order_grid table and update the values of that column from the appropriate column of sales_flat_order table.
In this case, Magento will automatically update this column in sales_flat_order_grid table for future orders.
The better way to display the boolean column is Yes/No renderer. Use the following code for this in _prepareColumns() method
$this->addColumn('exported', array(
'header' => Mage::helper('sales')->__('Exported'),
'index' => 'exported',
'type' => 'options',
'width' => '70px',
'options' => array(
1 => Mage::helper('sales')->__('Yes'),
0 => Mage::helper('sales')->__('No'),
),
));
There are some other useful articles about cutomizing order grid. Check out the links below:
http://inchoo.net/ecommerce/magento/how-to-extend-magento-order-grid/
http://www.ecomdev.org/2010/07/27/adding-order-attribute-to-orders-grid-in-magento-1-4-1.html
http://www.demacmedia.com/magento-commerce/mini-tutorial-adding-column-to-orders-grid-in-magento-backend/
http://www.atwix.com/magento/column-to-orders-grid/

how to get customer group from subscriber magento

As you know, in database, newsletter_subscriber table have customer_id column
In admin newsletter subscriber grid, i add column Customer Group like this
$this->addColumn('customer_group', array(
'header' => Mage::helper('newsletter')->__('Customer Group'),
'index' => 'customer_group'
));
So how can get customer group from customer_id in subscriber?
thks
modify last lines in protected function _prepareCollection() method
$collection->getSelect()->join(array('ce'=>'customer_entity'),'ce.entity_id=main_table.customer_id',array('ce.group_id'));
$collection->getSelect()->join(array('cg'=>'customer_group'),'cg.customer_group_id=ce.group_id',array('cg.customer_group_code'));
$this->setCollection($collection);
and then add field like this
$this->addColumn('customer_group_code', array(
'header' => Mage::helper('newsletter')->__('Customer Group'),
'index' => 'customer_group_code'
));

How to show Sum of Two fields in Grid in Magento Admin Panel

I am working on a extension in which user enter different price for " Stamp Cost, Ink Cost,
Form Cost ". currently in data grid i am showing value of one field
$this->addColumn('stamp_cost', array(
'header' => Mage::helper('imprint')->__('Stamp Cost'),
'width' => '100px',
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'stamp_cost'
));
But Now I Need to show sum of all these fields in one column
How can we show sum of two fields in one column in magento admin data grid ?
Essentially, there are two ways to do it. Add the field to the collection and get the data from the database, or calculate it in PHP based on the 3 values returned from the DB. Doing the first way with the Magento Collection would, in my opinion, be too complex. instead, you want to use a Renderer (Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract)
First, inside of the Block/Adminhtml folder of your plugin, make a new folder called Renderer. Inside of it make a new file called CostSum.php with the following contents:
<?php
class Company_Module_Block_Adminhtml_Renderer_CostSum extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
return $row->getStampCost() + $row->getInkCost() + $row->getFormCost();
}
}
Then, in the grid, make a new column
$this->addColumn('cost_total', array(
'header' => Mage::helper('imprint')->__('Stamp Cost'),
//'index' => 'Im not sure this is necessary',
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'renderer' => new Company_Module_Block_Adminhtml_Renderer_CostSum()
));
Hope that helps!
The more right way is 'renderer' => 'company_module/adminhtml_renderer_costSum'
As #Zyava says, the correct option is this. But actually, is not 'company_module'. Instead you should call it as you have declared your blocks in the config.xml file.
<blocks>
<declaration>
<class>Company_Module_Block</class>
</declaration>
</blocks>
So, in this case you should create the 'renderer' as:
'renderer' => 'declaration/adminhtml_renderer_costSum'

Magento - Add column to customer grid

how to add a custom column to magento customer grid ?
Thanks a lot.
You should override the class Mage_Adminhtml_Block_Customer_Grid (app/code/core/Mage/Adminhtml/Block/Customer/Grid.php) and apply the following changes:
1 - add the new attribute to show in the _prepareCollection() function
2 - add the new column to show in the _prepareColumns() function
Credit: http://www.leonhostetler.com/blog/magento-add-attribute-columns-in-manage-products-grid-201205/
Magento does not provide us with the ability to choose which attributes are included as columns in the Manage Products grid but it’s fairly simple to make the necessary code changes.
The code that generates the Manage Products grid is at /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php. The first thing you need to do is copy Grid.php into to the local directory structure. In other words, you copy Grid.php into the following location; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/. If there is no such location, then you must create the necessary directories. The final location of the file must be; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php
Now, open Grid.php (the one in the local directory structure) and begin editing. Find the following code;
$this->addColumn('sku',
array(
'header'=> Mage::helper('catalog')->__('SKU'),
'width' => '80px',
'index' => 'sku',
));
That’s the code that adds the SKU column to the product grid. Now, let’s say you have a custom attribute called Supplier ID (supplier_ID) and you want these to appear on the Manage Products grid as well. Place the following code either before or after the above block of code, as long as it’s inside _prepareColumns().
$this->addColumn('supplier_id',
array(
'header'=> Mage::helper('catalog')->__('Supplier ID'),
'width' => '150px',
'index' => 'supplier_id',
));
Then add the following line to _prepareCollection() where the other attributes are listed like this;
->addAttributeToSelect('supplier_id')
That should be all you need to do. You might have to re-compile, refresh your caches, logout, and log back in to see the change in your product grid.
The above example is for adding an attribute with a Catalog Input Type for Store Owner of Text Field. What if your attribute uses a dropdown list? The code above will have to be modified.
Let’s say you have an attribute called Supplier (supplier) which in the product editor presents a dropdown list of suppliers to choose from. To do this, we can add the following code to _prepareColumns():
$supplier_items =
Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id',
'attribute_code'); foreach ($supplier_items as $supplier_item) :
if ($supplier_item->getAttributeCode() == 'supplier')
$supplier_options[$supplier_item->getOptionId()] = $supplier_item->getValue(); endforeach; $this->addColumn('supplier',
array(
'header'=> Mage::helper('catalog')->__('supplier'),
'width' => '150px',
'type' => 'options',
'index' => 'supplier',
'options' => $supplier_options, ));
And let’s not forget to add the following line to _prepareCollection() where the other attributes are listed like this;
->addAttributeToSelect('supplier')
That should do it for you. Re-compile, refresh your caches, and logout and then back in if you need to.
I have publish here with real example.
If you need to add custome attribute, you may need to take care of join statements carefully.
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
Rewrite customer grid block with your custom module.
app/code/[local or community]/YourCompany/YourModule/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<yourcompany_yourmodule>
<version>0.1.0</version>
</yourcompany_yourmodule>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>YourCompany_YourModule_Block_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
app/code/[local or community]/YourCompany/YourModule/Block/Customer/Grid.php
<?php
class YourCompany_YourModule_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
public function setCollection($collection)
{
$collection->addAttributeToSelect('confirmation');
parent::setCollection($collection);
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$this->addColumn('confirmation', array(
'header'=> Mage::helper('sales')->__('Confirmed'),
'index' => 'confirmation',
'type' => 'text',
'width' => '100px',
));
return parent::_prepareColumns();
}
}
Detailed explanation can be found here:
http://tipsmagento.blogspot.com/2011/03/add-new-column-on-customers-grid.html
Make sure to check out TigerMin for Magento. It's a tool with which you can easily add columns to productsgrid and even inline edit values instantly. Here`s a live demo: http://demo.emvee-solutions.com/tigermin/

Resources