앞에서 언급 한 다른 솔루션으로 다시 시도해 보겠습니다. 그런 다음 그리드 페이지를 주문할 때 열을 추가하기 위해 레이아웃 업데이트 파일 만 있으면됩니다.
확장명을 Example_SalesGrid라고했지만 원하는대로 변경할 수 있습니다.
/app/etc/modules/Example_SalesGrid.xml에 모듈 init xml을 작성하여 시작해 보겠습니다 .
<?xml version="1.0" encoding="UTF-8"?>
<!--
Module bootstrap file
-->
<config>
<modules>
<Example_SalesGrid>
<active>true</active>
<codePool>community</codePool>
<depends>
<Mage_Sales />
</depends>
</Example_SalesGrid>
</modules>
</config>
다음으로 /app/code/community/Example/SalesGrid/etc/config.xml에 모듈 구성 xml을 만듭니다 .
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Example_SalesGrid>
<version>0.1.0</version> <!-- define version for sql upgrade -->
</Example_SalesGrid>
</modules>
<global>
<models>
<example_salesgrid>
<class>Example_SalesGrid_Model</class>
</example_salesgrid>
</models>
<blocks>
<example_salesgrid>
<class>Example_SalesGrid_Block</class>
</example_salesgrid>
</blocks>
<events>
<!-- Add observer configuration -->
<sales_order_resource_init_virtual_grid_columns>
<observers>
<example_salesgrid>
<model>example_salesgrid/observer</model>
<method>addColumnToResource</method>
</example_salesgrid>
</observers>
</sales_order_resource_init_virtual_grid_columns>
</events>
<resources>
<!-- initialize sql upgrade setup -->
<example_salesgrid_setup>
<setup>
<module>Example_SalesGrid</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
</example_salesgrid_setup>
</resources>
</global>
<adminhtml>
<layout>
<!-- layout upgrade configuration -->
<updates>
<example_salesgrid>
<file>example/salesgrid.xml</file>
</example_salesgrid>
</updates>
</layout>
</adminhtml>
</config>
이제 /app/code/community/Example/SalesGrid/sql/example_salesgrid_setup/install-0.1.0.php에 SQL 업그레이드 스크립트를 만듭니다 .
<?php
/**
* Setup scripts, add new column and fulfills
* its values to existing rows
*
*/
$this->startSetup();
// Add column to grid table
$this->getConnection()->addColumn(
$this->getTable('sales/order_grid'),
'customer_group_id',
'smallint(6) DEFAULT NULL'
);
// Add key to table for this field,
// it will improve the speed of searching & sorting by the field
$this->getConnection()->addKey(
$this->getTable('sales/order_grid'),
'customer_group_id',
'customer_group_id'
);
// Now you need to fullfill existing rows with data from address table
$select = $this->getConnection()->select();
$select->join(
array('order'=>$this->getTable('sales/order')),
$this->getConnection()->quoteInto(
'order.entity_id = order_grid.entity_id'
),
array('customer_group_id' => 'customer_group_id')
);
$this->getConnection()->query(
$select->crossUpdateFromSelect(
array('order_grid' => $this->getTable('sales/order_grid'))
)
);
$this->endSetup();
다음으로 /app/design/adminhtml/default/default/layout/example/salesgrid.xml에 레이아웃 업데이트 파일을 만듭니다.
<?xml version="1.0"?>
<layout>
<!-- main layout definition that adds the column -->
<add_order_grid_column_handle>
<reference name="sales_order.grid">
<action method="addColumnAfter">
<columnId>customer_group_id</columnId>
<arguments module="sales" translate="header">
<header>Customer Group</header>
<index>customer_group_id</index>
<type>options</type>
<filter>Example_SalesGrid_Block_Widget_Grid_Column_Customer_Group</filter>
<renderer>Example_SalesGrid_Block_Widget_Grid_Column_Renderer_Customer_Group</renderer>
<width>200</width>
</arguments>
<after>grand_total</after>
</action>
</reference>
</add_order_grid_column_handle>
<!-- order grid action -->
<adminhtml_sales_order_grid>
<!-- apply the layout handle defined above -->
<update handle="add_order_grid_column_handle" />
</adminhtml_sales_order_grid>
<!-- order grid view action -->
<adminhtml_sales_order_index>
<!-- apply the layout handle defined above -->
<update handle="add_order_grid_column_handle" />
</adminhtml_sales_order_index>
</layout>
이제 필터 옵션 /app/code/community/Example/SalesGrid/Block/Widget/Grid/Column/Customer/Group.php 를 만들기 위해 두 개의 차단 파일이 필요합니다 .
<?php
class Example_SalesGrid_Block_Widget_Grid_Column_Customer_Group extends Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Select {
protected $_options = false;
protected function _getOptions(){
if(!$this->_options) {
$methods = array();
$methods[] = array(
'value' => '',
'label' => ''
);
$methods[] = array(
'value' => '0',
'label' => 'Guest'
);
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt' => 0))
->load()
->toOptionArray();
$this->_options = array_merge($methods,$groups);
}
return $this->_options;
}
}
그리고 두 번째는 행 값을 표시 될 올바른 텍스트 /app/code/community/Example/SalesGrid/Block/Widget/Grid/Column/Renderer/Customer/Group.php로 변환합니다 .
<?php
class Example_SalesGrid_Block_Widget_Grid_Column_Renderer_Customer_Group extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
protected $_options = false;
protected function _getOptions(){
if(!$this->_options) {
$methods = array();
$methods[0] = 'Guest';
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt' => 0))
->load()
->toOptionHash();
$this->_options = array_merge($methods,$groups);
}
return $this->_options;
}
public function render(Varien_Object $row){
$value = $this->_getValue($row);
$options = $this->_getOptions();
return isset($options[$value]) ? $options[$value] : $value;
}
}
마지막으로 필요한 파일은 sales / order (sales_flat_order) 이외의 테이블에서 추가 열을 만드는 경우에만 필요합니다. sales / order_grid 테이블에서 sales / order의 열 이름과 일치하는 sales / order_grid의 모든 필드가 자동으로 업데이트됩니다. 예를 들어 지불 옵션을 추가해야하는 경우 데이터를 올바른 테이블에 복사 할 수 있도록 필드를 쿼리에 추가하려면이 옵저버가 필요합니다. 이를 위해 사용되는 관찰자는 /app/code/community/Example/SalesGrid/Model/Observer.php에 있습니다 .
<?php
/**
* Event observer model
*
*
*/
class Example_SalesGrid_Model_Observer {
public function addColumnToResource(Varien_Event_Observer $observer) {
// Only needed if you use a table other than sales/order (sales_flat_order)
//$resource = $observer->getEvent()->getResource();
//$resource->addVirtualGridColumn(
// 'payment_method',
// 'sales/order_payment',
// array('entity_id' => 'parent_id'),
// 'method'
//);
}
}
이 코드는 http://www.ecomdev.org/2010/07/27/adding-order-attribute-to-orders-grid-in-magento-1-4-1.html 의 예제를 기반으로합니다 .
위의 예가 문제를 해결하기를 바랍니다.