Magento - 如何在Orders网格过滤器中添加国家/地区列?

时间:2014-06-26 02:06:00

标签: magento

我试过了:

$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列。感谢。

2 个答案:

答案 0 :(得分:2)

复制到app/code/core/Mage/Adminhtml/Block/Sales/Order/Gridapp/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://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/

答案 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

总之,

下面创建的示例将国家/地区代码添加到销售订单网格页面上

  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>
  1. 创建插件以将列数据附加到报告结果集合。无论如何,您可以自定义插件查询以根据需要获取所需的数据。如果要进行更改,请确保还要在/app/code/Vendor/Module/view/adminhtml/uihtml/ui_component/sales_order_grid.xml中更新正确的数据库表列名称。

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

干杯!

相关问题