我试过了:
$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();
但结果是空白的。 请帮我在Orders网格中添加country列。感谢。
答案 0 :(得分:2)
复制到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/
答案 1 :(得分:0)
此外,我建议您也参考以下文章。肯定会帮助您。
https://www.mageworx.com/blog/how-to-add-column-with-filter-to-magento-2-orders-grid/ https://www.magentoexpertise.in/shipping-country-column-on-the-sales-order-grid/ https://belvg.com/blog/how-to-add-column-in-sales-order-grid-in-magento-2-1.html
总之,
下面创建的示例将国家/地区代码添加到销售订单网格页面上
/app/code/Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
File Signature
-->
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="country_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
<item name="label" xsi:type="string" translate="true">Country Code (Ex: AU or NZ)</item>
<item name="sortOrder" xsi:type="number">60</item>
<item name="align" xsi:type="string">left</item>
<item name="dataType" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="filter" xsi:type="string">text</item>
</item>
</argument>
</column>
</columns>
</listing>
/app/code/Vendor/Module/etc/adminhtml/di.xml
<?xml version="1.0"?>
<!--
File Signature
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<plugin name="vendor_module_plugin_view_element_uicomponent_dataprovider_collectionfactory"
type="Vendor\Module\Plugin\PluginAddDataToSalesOrdersGrid"
sortOrder="1"
disabled="false"/>
</type>
</config>
/app/code/Vendor/Module/Plugin/PluginAddDataToSalesOrdersGrid.php
<?php
/**
* File Signature
*/
namespace Vendor\Module\Plugin;
/**
* Class PluginAddDataToSalesOrdersGrid
*/
class PluginAddDataToSalesOrdersGrid
{
/**
* Execute after the getReport function to
* Append additional data to sales order grid
* @param \Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject
* @param \Magento\Sales\Model\ResourceModel\Order\Grid\Collection $resultCollection
* @param $requestName
* @return mixed
*/
public function afterGetReport($subject, $resultCollection, $requestName)
{
if ($requestName !== 'sales_order_grid_data_source') {
return $resultCollection;
}
if ($resultCollection->getMainTable() === $resultCollection->getResource()->getTable('sales_order_grid')) {
try {
$orderAddressTableName = $resultCollection->getResource()->getTable('sales_order_address');
$resultCollection->getSelect()->joinLeft(
['soa' => $orderAddressTableName],
'soa.parent_id = main_table.entity_id AND soa.address_type = \'shipping\'',
['soa.country_id']
);
} catch (Exception $e) {
// Not required to log and added the try catch to make sure
// flow is not breaking while processing the report
}
}
return $resultCollection;
}
}
干杯!