我试过:
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->oinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
$this->setCollection($collection);
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
但结果是空白。 请帮助我在订单网格中添加国家列。谢谢。
最佳答案
复制到 app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid
到 app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid
在文件中添加以下代码,用于将账单地址附加到订单网格以用于 prepareCollection
函数
$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('telephone','city','postcode','country_id' ) )->where("sales_flat_order_address.address_type = 'billing'");
完整代码为
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('telephone','city','postcode','country_id' ) )->where("sales_flat_order_address.address_type = 'billing'");
$this->setCollection($collection);
return parent::_prepareCollection();
}
然后将下面的代码添加到_prepareColumns()
$this->addColumn('country_id', array(
'header' => Mage::helper('sales')->__('Country Id'),
'index' => 'country_id',
'filter_index' => 'sales_flat_order_address.country_id',
));
如果想要国家列表然后添加下面的代码 _prepareCollection()
$this->addColumn('country_id', array(
'header' => Mage::helper('sales')->__('Country Id'),
'index' => 'country_id',
'type'=> 'options',
'options'=>$this->getAllCountry(),
'filter_index' => 'sales_flat_order_address.country_id',
));
然后在这个文件上添加新的函数
public function getAllCountry(){
$options = Mage::getResourceModel('directory/country_collection')->load()->toOptionArray();
$countries = array();
foreach($options as $options){
$countries[$options['value']]=$options['label'];
}
return $countries;
}
http://inchoo.net/ecommerce/magento/how-to-extend-magento-order-grid/
关于Magento - 如何在订单网格过滤器中添加国家列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24421355/