Add custom column to Magento report and sales panel

I was looking for a module or a way to handle the situation:

A customer orders a product worth $ 100 and pays $ 10. This will require him 110 dollars. By the time the product arrives, he notices that the product is slightly scratched, and instead of sending it back, he accepts a discount.

For this to happen properly, I would make a credit note with a corrective return let say $ 30.

I need to see the total amount left after this operation ($ 80) in a separate column in the reports or on the sales control panel.

For this specific task, we also installed a module called "Advanced Orders Manager from Iksanika", but apparently it only receives data that already exists in the database, and does not allow us to use the variable for let say substraction.

Also in Magento reports we use Reports> Sales> Orders, but this gives us only total figures, and we can not find anywhere the "total amount" that will give us the exact figure ($ 80).

This is a special request for an account in an online store.

+5
source share
1 answer

What you want to do is add a custom grid column to the sales order grid.

Magento uses the sales_flat_order database table to populate the values ​​in the sales order grid. Unfortunately, the sales_flat_order table does not provide the information you need (grant minus refunded amount), but both values ​​are separate (grand_total and total_refunded). Because of this, you have several options:

  • Expand the table sales_flat_order.
  • Add a custom renderer for a custom order column column.

Like everything in this world, both methods have advantages and disadvantages.

If you expand the sales_flat_order table, you must ensure that the value of the new database column is set when creating new orders. On the other hand, since the value is stored in your database, you can use the column for other extensions.

With a custom visualization tool, you don’t have to worry about saving. You have the opportunity to perform operations and return your result, which will be displayed in the column of the custom column of the sales order. Since we saved both grand_total and total_refunded, you can return a grand minus refunded amount.

I will describe how to add a special order grid column and add my own renderer to return the grandtotal value minus the refund amount.

Part 1: How to add a custom column to the sales order grid in the backend?

To add a custom column to the 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 expand it with the sales order grid.

To add your own _prepareColumns () column override method. Inside the rewritten _prepareColumns (), you want to first add the column from the parent class. Finally, add your own column:

Note: you can download the sample 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 of a sales order configuration 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')->__('GT (Base)'), 'index' => 'base_grand_total', 'type' => 'currency', 'currency' => 'base_currency_code', )); $this->addColumn('grand_total', array( 'header' => Mage::helper('sales')->__('GT (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 my own renderer for my custom column?

Now you can add your own renderer to fill in the values ​​in the column of the custom column of the sales order.

First add the cusom XX_ModuleName_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_MyColumn rendering class.

Then add Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract .

Cancel the render method (Varien_Object $ row) . Here you can perform your specific operation and return the row that should be displayed in your grid. In your case, you want to load a collection of orders for the current parameter $ row, get the total refund amount, subtract the grant with the returned amount and return the value.

An example of a custom render 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 sample module .

+4
source

Source: https://habr.com/ru/post/1237355/


All Articles