Magento 2如何将新列添加到订单网格


20

我已经在sales_order名为的表中添加了一个新列export_status,现在我想使用新列中的数据添加新订单网格sales_order列。

我设法添加列到the sales_order_grid表中。

$installer->getConnection()->addColumn($installer->getTable("sales_order_grid"), "xml_exported", [
     'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
     'comment' => 'XML Exported'
]);

现在如何使它与sales_order export_status列中的值一起实际显示在订单网格中?



我认为有一种更好的方法,而不必实现观察者和使用事件。
安德烈·费拉兹(AndréFerraz)2016年

:本文介绍一步一步增加销售订单网格列sivaschenko.com/magento2/2016/03/05/...
谢尔盖·伊瓦先科

此处尝试解决方案此方法使用插件添加列。
Asrar

我认为您应该检查一下:很好的教程sivaschenko.com/magento2-sales-order-grid-column
Abid Malik

Answers:


42

在大量研究了magento核心代码之后,我找到了一个解决问题的方法。代替经由数据库添加到网格的列的,我创建UI组件sales_order_grid.xml[COMPANY]/[MODULE]/view/adminhtml/ui_component/sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="export_status" class="[COMPANY]\[MODULE]\Ui\Component\Listing\Column\Status">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">XML Exported</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

然后在下面创建UI类 [COMPANY]/[MODULE]/Ui/Component/Listing/Column/Status.php

namespace [COMPANY]\[MODULE]\Ui\Component\Listing\Column;

use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;

class Status extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;

    public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $criteria, array $components = [], array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {

                $order  = $this->_orderRepository->get($item["entity_id"]);
                $status = $order->getData("export_status");

                switch ($status) {
                    case "0":
                        $export_status = "No";
                        break;
                    case "1";
                        $export_status = "Yes";
                        break;
                    default:
                        $export_status = "Failed";
                        break;

                }

                // $this->getData('name') returns the name of the column so in this case it would return export_status
                $item[$this->getData('name')] = $export_status;
            }
        }

        return $dataSource;
    }
}

这很棒。我遇到的问题是,这种自定义导致订单网格的工具栏在网格之后呈现。您遇到这个问题了吗?
2013年

@Ian我没有那个问题。您正在使用哪个版本的magento 2?
安德烈·费拉斯

2
CE 2.1.1。我确定了问题。我的模块按字母顺序排在Magento_Sales之前。我向Magento_Sales添加了具有依赖性的<sequence>,禁用了我的模块,然后重新启用了它。这解决了问题。
伊恩(Ian)

1
这对导出选项也起作用吗?在我的情况下,导出的数据具有空值。
MagePsycho

1
同样filter_condition_callback,您可以在Magento 1的每个字段中添加自己的自定义过滤器。请不要说“你是什么意思?” 这次;)
MagePsycho

33

我的解决方案可能会更有效率,因为它遵循在网格中添加列的本机方法,并且您不必加载页面上显示的每个订单即可获取列数据(而是使用sales_order_grid表)。上面是如何创建自定义渲染器的一个很好的例子。

供应商/ [供应商] / [包装] /view/adminhtml/ui_component/sales_order_grid.xml

<!-- Add the column "my_column" (My Column) to the sales order grid. -->
<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="my_column">
        <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">My Column</item>
            </item>
        </argument>
    </column>
</columns>

请注意,您必须使用订单网格上方的下拉小部件启用该列,然后该列才会显示。

供应商/ [供应商] / [包] /etc/di.xml

<!-- Sync the column "my_column" between the sales_order and sales_order_grid tables -->
<virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
    <arguments>
        <argument name="columns">
            <item name="my_column" xsi:type="string">sales_order.my_column</item>
        </argument>
    </arguments>
</virtualType>

将列添加到sales_order_grid表中的示例设置代码(在安装/升级脚本中使用此代码)。顺便说一句,相同的代码适用于sales_order表。

$setup->getConnection()->addColumn(
        $setup->getTable('sales_order_grid'),
        'my_column',
        [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'length' => 255,
            'nullable' => true,
            'comment' => 'My Column'
        ]
    );

希望能帮助到你!:-)


这个答案要求该列是sales_order_grid表,对吗?
Tero Lahtinen

@MageParts,此脚本何时起作用?
SagarPPanchal

1
为我工作,谢谢:3 btw,因为sales_order_grid.xml您应该关闭<listing> </ listing>标记,否则它将返回xml错误
fudu

在di:xml文件中,应将<argument name =“ columns”>替换为<argument name =“ columns” xsi:type =“ array”>。否则将返回以下错误。无效的文档元素“参数”:类型定义是抽象的。
saiid

1

为了在订单网格中创建新列,我参考了Magento默认模块vendor / magento / module-customer-balance

就我而言,“ custom_column”列已存在于sales_order表中。

我必须在订单网格中显示“ custom_column”列

步骤1:在sales_order_grid表中添加新列

        $connection = $installer->getConnection();
        if ($connection->isTableExists('sales_order_grid')) {
            $connection->addColumn(
                $connection->getTableName('sales_order_grid'),
                'custom_column',
                [
                    "type"      =>  Table::TYPE_TEXT,
                    'default'   =>  null,
                    "comment"   =>  'Custom Column'
                ]
            );
    }

第2步:app \ code [Vendor] [NameSpace] \ etc \ di.xml中的di.xml文件

 <!-- Adding Columns in sales_order_grid-->
    <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="custom_column" xsi:type="string">sales_order.custom_column</item>
            </argument>
        </arguments>
    </virtualType>
    <virtualType name="Magento\SalesArchive\Model\ResourceModel\Order\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="custom_column" xsi:type="string">sales_order.custom_column</item>
            </argument>
        </arguments>
    </virtualType>

步骤3:在app \ code [Vendor] [NameSpace] \ view \ adminhtml \ ui_component下创建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="custom_column">
            <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">Custom Column</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

注意:已在Magento 2.2.6版中测试过


0

演示/ExtendedGrid/view/adminhtml/ui_component/sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="new_column_name" class="Demo\ExtendedGrid\Ui\Component\Listing\Column\NewColumnStatus">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="boolean">false</item>
                    <item name="has_preview" xsi:type="string">1</item>
                    <item name="label" xsi:type="string" translate="true">New Column Name</item>
              </item>
            </argument>
        </column>
    </columns>
</listing>

然后创建UI类以呈现它。

演示/ ExtendedGrid / Ui /组件/列表/列/NewColumnStatus.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Demo\ExtendedGrid\Ui\Component\Listing\Column;

/**
 * Class  NewColumnStatus
 */
class NewColumnStatus extends \Magento\Ui\Component\Listing\Columns\Column
{

    /**
     * @var string[]
     */
    protected $statuses;

   /**
     * Constructor
     *
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param CollectionFactory $collectionFactory
     * @param array $components
     * @param array $data
     */
       public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
        \Magento\Framework\UrlInterface $urlBuilder,
        array $components = [],
        array $data = []
    ) {
        $this->urlBuilder = $urlBuilder;
         parent::__construct($context, $uiComponentFactory, $components, $data);
    }


    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return void
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {

           //put  the logic here


            }
        }

        return $dataSource;
   }
}

不要忘记清除缓存。


-1

我已经创建了自定义模块,该模块将在销售订单实体中添加自定义属性,然后在管理销售订单网格中显示。

用于定义模块的module.xml文件。app \ code \ COMPANY \ MODULE \ etc \ module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="COMPANY_MODULE" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

app \ code \ COMPANY \ MODULE \ etc \ di.xml中的di.xml文件

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="export_status" xsi:type="string">sales_order.export_status</item>
            </argument>
        </arguments>
    </virtualType>
</config>

InstallData.php在数据库表中添加自定义属性export_status

app \ code \ COMPANY \ MODULE \ Setup \ InstallData.php

<?php

namespace COMPANY\MODULE\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        if (version_compare($context->getVersion(), '1.0.0') < 0){      
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $salesSetup = $objectManager->create('Magento\Sales\Setup\SalesSetup');
            $salesSetup->addAttribute('order', 'export_status', ['type' =>'varchar']);
            $quoteSetup = $objectManager->create('Magento\Quote\Setup\QuoteSetup'); 
        }
    }
}

sales_order_grid.xml在销售订单管理网格中添加自定义列。

<?xml version="1.0" encoding="UTF-8"?>
<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="export_status">
            <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">XML Exported</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

您可以通过单击以下链接http://vdcstaging.co.in/download/add-custom-culumn-to-admin-grid.zip下载完整模块。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.