Magento 2.3 - Admin create order customer grid add company name
-
14-04-2021 - |
質問
I want to add the company name to the customer grid, when you create a new order in the admin panel.
Therefore I created the following module, but there is no field with company name added to the grid.
What am I missing here?
app/code/Vendor/Module/view/adminhtml/layout/sales_order_create_customer_block.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="adminhtml.customer.grid.container">
<arguments>
<argument name="dataSource" xsi:type="object">\Vendor\ Module\Model\ResourceModel\Order\Customer\Collection</argument>
</arguments>
</referenceBlock>
<referenceBlock name="adminhtml.customer.grid.columnSet">
<block class="Magento\Backend\Block\Widget\Grid\Column" as="company" after="billing_postcode">
<arguments>
<argument name="header" xsi:type="string" translate="true">Company</argument>
<argument name="index" xsi:type="string">company</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
app/code/Vendor/Module/Model/ResourceModel/Order/Customer/Collection.php
<?php
namespace Vendor\Module\Model\ResourceModel\Order\Customer;
class Collection extends \Magento\Sales\Model\ResourceModel\Order\Customer\Collection
{
/**
* @return $this
*/
protected function _initSelect()
{
parent::_initSelect();
$this->joinAttribute(
'company',
'customer_address/company',
'default_billing',
null,
'left'
);
return $this;
}
}
解決
step 1: add your column into the ui_component file
sales_order_grid.xml
app/code/Mbs/CompanyName/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="company" >
<settings>
<sortable>true</sortable>
<filter>text</filter>
<label translate="true">Company</label>
</settings>
</column>
</columns>
</listing>
step 2: create di.xml
app/code/Mbs/CompanyName/etc/di.xml
<?xml version="1.0" encoding="utf-8" ?>
<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="grid_order_company_name" type="Mbs\CompanyName\Plugin\JoinCompanyName" sortOrder="5" />
</type>
</config>
step 3: create an admin plugin that feeds the column with the value from the sales_order_address table
app/code/Mbs/CompanyName/Plugin/JoinCompanyName.php
<?php
namespace Mbs\CompanyName\Plugin;
use Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory;
class JoinCompanyName
{
public function afterGetReport(
CollectionFactory $subject,
$collection,
$requestName
) {
if ($requestName == 'sales_order_grid_data_source') {
$select = $collection->getSelect();
$select->joinLeft(
['customer_address' => $collection->getTable('sales_order_address')],
'main_table.entity_id = customer_address.parent_id and address_type="billing"',
['company']
);
}
return $collection;
}
}
Full module below: https://bitbucket.org/magstaging/companyname/src/master/
他のヒント
To resolve this issues, we can separate the problem in 2 steps:
step 1: make the column appearing
create a new module and add the layout file sales_order_create_customer_block.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="adminhtml.customer.grid.columnSet">
<block class="Magento\Backend\Block\Widget\Grid\Column" name="adminhtml.customer.grid.columnSet.company" as="Company">
<arguments>
<argument name="header" xsi:type="string" translate="true">Company</argument>
<argument name="index" xsi:type="string">billing_company</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
step2: feed the column with data take over the collection that feed the column
insert the xml snippet
<referenceBlock name="adminhtml.customer.grid.container">
<arguments>
<argument name="dataSource" xsi:type="object">Mbs\CompanyName\Model\ResourceModel\Order\Customer\Collection</argument>
</arguments>
</referenceBlock>
and then add the collection file
<?php
namespace Mbs\CompanyName\Model\ResourceModel\Order\Customer;
class Collection extends \Magento\Customer\Model\ResourceModel\Customer\Collection
{
/**
* @return \Magento\Sales\Model\ResourceModel\Order\Customer\Collection
*/
protected function _initSelect()
{
parent::_initSelect();
$this->joinAttribute(
'billing_company',
'customer_address/company',
'default_billing',
null,
'left'
);
return $this;
}
}