1

My scenario each product related to one merchant (simplest)

product have attribute merchant_id (int). This attribute not describe product, only field link to merchant table. merchan_id field associated with Merchant model

I have table merchant contain fields : entity_id (int -> value in product), merchant_id (unique string)

After magento 2.3.2 core team have updated

https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Catalog/Ui/DataProvider/Product/ProductCollection.php#L57

my field cannot filter anymore. So how is the correct way to add merchant_identifier field to product listing data ?

My Approach

1 Create field to catalog product listing

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="product_columns">
    <column name="merchant_identifier">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="add_field" xsi:type="boolean">true</item>
                <item name="sorting" xsi:type="string">asc</item>
                <item name="editor" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Merchant Identifier</item>
                <item name="sortOrder" xsi:type="number">10</item>
            </item>
        </argument>
    </column>
</columns>

2 Preference catalog etc/adminhtml/di.xml

<preference for="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider" type="Vendor\CatalogMerchants\Ui\DataProvider\Product\ProductDataProvider" />

3 Add my Provider extend Magento Provider

class ProductDataProvider extends \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider

in construct add join only

public function __construct(
    //Dependencies
) {
    /*Join with merchant table.*/
    $this->collection->joinTable(
        ['merchant_tbl' => $this->collection->getTable('merchant')],
        "entity_id=merchant_id",
        ['merchant_identifier'=>'merchant_id'],
        null,
        'left'
    );
}

Here is how field show in catalog product listing

+-----------+------------+---------------------+
| entity_id | product_id | merchant_identifier |
+-----------+------------+---------------------+
|         1 |     123453 | merchant_ide123A_Af |
|         2 |     654657 | merchant_ide213A_Af |
|         3 |     345477 | merchant_ide312A_Af |
|         4 |     114673 | merchant_ide231A_Af |
+-----------+------------+---------------------+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.