I want to add new column to customer grid and that new column is from my custom new table
how to do it in Magento2
customer-gridgridmagento2uicomponent
I want to add new column to customer grid and that new column is from my custom new table
how to do it in Magento2
The error shows that main_table.volume_id
does not exist, because main_table
is the alias for the product entity table which does not contain a column volume_id
.
You have defined volIds
as alias for your volume_product
association table. So you need to change it
$collection->getSelect()->join(array('volumes' => $volsTable),
'main_table.volume_id = volumes.entity_id', array('volume_name'));
$collection->getSelect()->join(array('volumes' => $volsTable),
'volIds.volume_id = volumes.entity_id', array('volume_name'));
Magento 2 add custom columns to sales order grid,
To join
Magento\Sales\Order\Grid\Collection
to any table(s), using plugin would be the best option as this does not rely on rewrites and makes code lean.
Create the plugin in your module's etc/di.xml
<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>
So, we are intercepting
Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory
because if you take a look at the
Magento\Sales\etc\di.xml
you would see
Magento\Sales\Order\Grid\Collection
was injected into
Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory
Create a Plugin folder and a plugin class in your 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;
}
}
Here we are observing around event of getReport() method.
Copy
vendor/magento/module-sales/view/adminhtml/ui_component/sales_order_grid.xml
to your module's scope
Vendor/ModuleName/view/adminhtml/ui_component/sales_order_grid.xml
Delete all contents of your copied sales_order_grid.xml as we don't want to override all the contents.
Enter the following code in your module's 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">
<!-- 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>
Now, delete cache from var/cache folder or refresh your cache. You would be able to see your added columns in is sales order grid.
Best Answer
to show custom column in customer grid i followed steps
view/adminhtml/ui/customer_listing.xml
Setp 2. add below code etc/Adminhtml/di.xml
Step 3. Join the table in collection
Note :- make sure custom_fk should set to is_used_in_grid=1 in customer_eav_attribute
if not set it to 1 and do the reindex