Magento 2 ajoute des colonnes personnalisées à la grille de commande client,
Joindre
Magento \ Ventes \ Commande \ Grille \ Collection
à n'importe quelle table, l'utilisation du plugin serait la meilleure option car cela ne dépend pas des réécritures et rend le code plus léger.
Créez le plugin dans etc / di.xml de votre module
<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>
Donc, nous interceptons
Magento \ Framework \ View \ Element \ UiComponent \ DataProvider \ CollectionFactory
parce que si vous regardez la
Magento \ Sales \ etc \ di.xml
tu verrais
Magento \ Ventes \ Commande \ Grille \ Collection
a été injecté dans
Magento \ Framework \ View \ Element \ UiComponent \ DataProvider \ CollectionFactory
Créez un dossier de plugin et une classe de plugin dans votre module
<?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(
["soi" => "sales_order_item"],
'main_table.entity_id = soi.order_id AND soi.product_type="simple"',
array('weight', 'product_type')
)
->distinct();
$select->join(
["soa" => "sales_order_address"],
'main_table.entity_id = soa.parent_id AND soa.address_type="shipping"',
array('email', 'country_id', 'postcode', 'city', 'telephone')
)
->distinct();
}
}
return $this->collection;
}
}
Ici, nous observons autour de l' événement de la méthode getReport ().
Copie
fournisseur / magento / module-sales / view / adminhtml / ui_component / sales_order_grid.xml
à la portée de votre module
Fournisseur / ModuleName / view / adminhtml / ui_component / sales_order_grid.xml
Supprimez tout le contenu de votre sales_order_grid.xml copié car nous ne voulons pas remplacer tout le contenu.
Entrez le code suivant dans sales_order_grid.xml de votre module
<?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">
<!-- sales_order_item weight -->
<column name="weight">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Weight</item>
<item name="sortOrder" xsi:type="number">222</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_item product_type-->
<column name="product_type">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Vendor\ModuleName\Ui\Component\Listing\Column\ProductTypes</item>
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Product Type</item>
<item name="sortOrder" xsi:type="number">232</item>
<item name="align" xsi:type="string">right</item>
<!--<item name="filter" xsi:type="string">select</item>-->
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
<item name="dataType" xsi:type="string">select</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_address country_id -->
<column name="country_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Country ID</item>
<item name="sortOrder" xsi:type="number">242</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_address post_code -->
<column name="postcode">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Postcode</item>
<item name="sortOrder" xsi:type="number">252</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_address city -->
<column name="city">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">City</item>
<item name="sortOrder" xsi:type="number">252</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_address telephone -->
<column name="telephone">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Telephone</item>
<item name="sortOrder" xsi:type="number">252</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
</columns>
</listing>
Maintenant, supprimez le cache du dossier var / cache ou actualisez votre cache. Vous pourrez voir vos colonnes ajoutées dans la grille de commande client.
["soi" => "sales_order_item"]
et["soa" => "sales_order_address"]
.