Magento - 如何在订单网格过滤器中添加国家列?

3

我尝试过:

$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();

但是结果为空。 请帮我在订单网格中添加国家列。谢谢。

这段程序相关的内容是:$collection->oinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left'); 这是一个打字错误还是你真的漏掉了 j。正确应该是 $collection->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left'); - MTM
2个回答

2
复制到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',
        ));

如果想要国家列表,请添加以下代码_preapareCollection()。
 $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://bluehorse.in/blog/how-to-add-some-field-or-column--into-magento-order-grid-in-magento-or-customized-magento-order-grid.html

http://inchoo.net/ecommerce/magento/how-to-extend-magento-order-grid/


谢谢您的回复,您说得对,但它只显示国家ID,我想显示国家名称。您能再帮我一次吗? - hu-xeko
非常感谢,我已经完成了,感谢您的建议。 - hu-xeko
1
$options = Mage::getResourceModel('directory/country_collection')->load()->toOptionArray(); $countries = array(); foreach($options as $option){ $countries[$option['value']]=$option['label']; } 没问题 :) - hu-xeko

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

总之,
以下示例旨在在销售订单网格页面上添加国家代码
  1. 创建一个UI组件以添加新列(列名称 - 例如:country_id必须与数据库表列名称相同)

/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>
  1. 添加一个插件,将附加列数据追加到销售订单网格表列中

/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/view/adminhtml/ui_component/sales_order_grid.xml中更新正确的DB表列名称。

/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;
    }
}

干杯!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接