当前位置: 技术文章>> 如何在Magento 2的订单列表中添加送货地址详细信息

文章标题:如何在Magento 2的订单列表中添加送货地址详细信息
  • 文章分类: Magento
  • 18618 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


在Magento 2的订单网格中添加送货地址详细信息的步骤:

步骤1:首先,我们需要在扩展名中创建一个sales_order_grid.xml文件,路径如下

app\code\Vendor\Extension\view\adminhtml\ui_component\

并添加如下所述的代码

<?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">
        <settings>
            <childDefaults>
                <param name="fieldAction" xsi:type="array">
                    <item name="provider" xsi:type="string">false</item>
                </param>
            </childDefaults>
        </settings>
        <column name="street">
            <settings>
                <filter>text</filter>
                <label translate="true">Street</label>
                <bodyTmpl>ui/grid/cells/html</bodyTmpl>
                <visible>true</visible>
            </settings>
        </column>
        <column name="postcode">
            <settings>
                <filter>text</filter>
                <label translate="true">Zip</label>
                <bodyTmpl>ui/grid/cells/html</bodyTmpl>
                <visible>true</visible>
            </settings>
        </column>
        <column name="city">
            <settings>
                <filter>text</filter>
                <label translate="true">City</label>
                <bodyTmpl>ui/grid/cells/html</bodyTmpl>
                <visible>true</visible>
            </settings>
        </column>
        <column name="telephone">
            <settings>
                <filter>text</filter>
                <label translate="true">Phone</label>
                <bodyTmpl>ui/grid/cells/html</bodyTmpl>
                <visible>true</visible>
            </settings>
        </column>
    </columns>
</listing>

步骤2: 之后,我们需要在扩展文件夹中的以下给定路径中创建一个di.xml文件:

app\code\Vendor\Extension\etc\

现在,添加以下代码

<?xml version="1.0"?>
<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="custom_orders_grid" type="Vendor\Extension\Plugin\OrdersGrid" sortOrder="10"/>
    </type>
</config>

步骤3:之后,我们需要在下面的路径文件夹中创建OrdersGrid.php文件,以将文件添加到模块中。

app\code\Vendor\Extension\Plugin\

现在,添加以下代码

<?php
namespace Vendor\Extension\Plugin;
 
class OrdersGrid
{
    public function afterGetReport($subject, $collection, $requestName)
    {
        if ($requestName !== 'sales_order_grid_data_source')
        {
            return $collection;
        }
 
        if ($collection->getMainTable() === $collection->getResource()->getTable('sales_order_grid'))
        {
            $orderAddressTable  = $collection->getResource()->getTable('sales_order_address');
 
            $collection->getSelect()->joinLeft(
                ['oat' => $orderAddressTable],
                'oat.parent_id = main_table.entity_id AND oat.address_type = \'shipping\'',
                ['telephone', 'city', 'postcode', 'street']
            );
        }
 
        return $collection;
    }
}

完成上述步骤后,请在Magento 2商店后端中检查结果。送货地址详细信息列,如街道,邮政编码,城市,电话已添加到Magento 2的订单网格中。

结论:

因此,通过这种方式,您可以在Magento 2的订单网格中添加送货地址详细信息。


推荐文章