Magento 2将自定义字段添加到销售订单导出csv问题

时间:2018-05-09 06:16:22

标签: magento magento2

我在销售订单网格中添加了跟踪编号列。它在销售订单网格中显示正常。但是当我通过csv导出数据时,发生了以下错误。

  

您无法定义相关名称' sst'不止一次

我已实施以下步骤。

将自定义列添加到sales_order_grid.xml

<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="track_number">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">                   
                    <item name="filter" xsi:type="string">text</item>                    
                    <item name="label" xsi:type="string" translate="true">Tracking Number
                    </item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

并通过di.xml文件中的插件覆盖 sales_order_additional_columns 功能

<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="sales_order_additional_columns" type="Vendor\ModuleName\Plugins\AddColumnsSalesOrderGridCollection" sortOrder="100" disabled="false" />
    </type>
</config>

覆盖销售订单网格的集合。

<?php 
namespace Vendor\Modulename\Plugins;

use Magento\Framework\Message\ManagerInterface as MessageManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;

class AddColumnsSalesOrderGridCollection
{
    private $messageManager;
    private $collection;

    public function __construct(MessageManager $messageManager,
        SalesOrderGridCollection $collection
    ) {

        $this->messageManager = $messageManager;
        $this->collection = $collection;
    }

    public function aroundGetReport(
        \Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject,
        \Closure $proceed,
        $requestName    
    ) {        
        $result = $proceed($requestName);
        if ($requestName == 'sales_order_grid_data_source') {
            if ($result instanceof $this->collection
            ) { 
                $select = $this->collection->getSelect();
                $select->join(
                    ["sst" => "sales_shipment_track"],
                    'main_table.entity_id = sst.entity_id',
                    'sst.track_number'
                )
                    ->distinct();                                   
            }

        }
        return $this->collection;
    }
}

1 个答案:

答案 0 :(得分:0)

join在导出过程中发生了两次,为避免这种情况,请在注册表中添加检查:

use Magento\Framework\Message\ManagerInterface as MessageManager;
use Magento\Framework\Registry;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;


    class AddColumnsSalesOrderGridCollection
    {
        private $messageManager;
        private $collection;
        private $registry;

        public function __construct(MessageManager $messageManager,
                                    SalesOrderGridCollection $collection,
                                    Registry $registry
        )
        {

            $this->messageManager = $messageManager;
            $this->collection = $collection;
            $this->registry = $registry;
        }

        public function aroundGetReport(
            \Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject,
            \Closure $proceed,
            $requestName
        )
        {
            $result = $proceed($requestName);
            if ($requestName == 'sales_order_grid_data_source') {
                if ($result instanceof $this->collection
                ) {
                    if (is_null($this->registry->registry('shipment_joined'))) {
                        $select = $this->collection->getSelect();
                        $select->join(
                            ["sst" => "sales_shipment_track"],
                            'main_table.entity_id = sst.entity_id',
                            'sst.track_number'
                        )
                            ->distinct();
                        $this->registry->register('shipment_joined', true);
                    }
                }

            }
            return $this->collection;
        }
    }