您想要做的是將自定義網格列添加到銷售訂單網格。
Magento使用數據庫表sales_flat_order填寫銷售訂單網格中的值。不幸的是,sales_flat_order表沒有提供您需要的信息(grandtotal減去退款金額),但兩個值分開(grand_total和total_refunded)。因爲,你有多種選擇:
- 擴展表 sales_flat_order。
- 爲您的自定義銷售訂單網格列添加自定義渲染器。
因爲這個世界上的一切都有優點和缺點。
如果您擴展sales_flat_order表,您必須確保在創建新訂單時設置新數據庫列的值。另一方面,由於該值在數據庫中保存,因此可以將該列用於其他擴展。
使用自定義渲染器,您不必關心持久性。您有機會進行操作並返回將顯示在自定義銷售訂單網格列中的結果。因爲我們已經保存了grand_total和total_refunded,您可以返回grandtotal減去退款金額。
我將介紹如何添加自定義銷售訂單網格列並添加自定義渲染器以返回grandtotal減去退款金額的值。
第1部分:如何將自定義列添加到後端的銷售訂單網格?
要添加自定義列到銷售訂單電網第一添加自己的銷售訂單網格塊XX_ModuleName_Block_Adminhtml_Order_Grid。
重寫magentos ** Mage_Adminhtml_Block_Sales_Order_Grid *(app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
)並使用您的銷售訂單網格擴展它。
要添加您的自定義列覆蓋_prepareColumns()方法。在覆蓋的_prepareColumns()中,您需要首先添加父類的列。最後,添加您的自定義列:
注意:您可以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'
));
實例等/ 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>
示例自定義銷售訂單網格塊:
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();
}
}
第2部分:如何添加自定義渲染我的自定義列?
現在您可以添加自定義渲染器以將值填充到自定義銷售訂單網格列中。
首先添加一個cusom渲染器類XX_ModuleName_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_MyColumn。
然後延伸Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract。
覆蓋渲染(Varien_Object $ row)方法。在這裏,您可以執行特定操作並返回應顯示在網格中的字符串。在你的情況下,你想要加載目前$ row param的訂單集合,獲得總退款金額,用退款金額減去grandtotal並返回該值。
例自定義渲染塊:
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;
}
}
你可以download the example module。
你想編碼你自己的解決方案,或者你只是在尋找一個擴展? – Enigmativity
@Enigmativity要麼做 – Mike