0

I am trying to override Related in the product edit to remove it but it does not work

In my file etc/adminhtml/di.xml

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="roles-related" xsi:type="array">
                    <item name="class" xsi:type="string">YourVendor\YourModule\Ui\DataProvider\Product\Form\Modifier\Related</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

In my file php

<?php

namespace YourVendor\YourModule\Ui\DataProvider\Product\Form\Modifier;

use Magento\Ui\Component\Form\Element\Input;
use Magento\Ui\Component\Form\Field;
use Magento\Ui\Component\Form\Fieldset;

class Related extends \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Related
{
    public function modifyMeta(array $meta)
    {
        $checkNumber = 1;

        if ($checkNumber == 1) {
            unset($meta[static::GROUP_RELATED]);
        }

        return $meta;
    }
}

I hope someone can help with this problem.

1 Answer 1

2

Your modifier does not remove the Related config from the $meta because its sort order is smaller than the sort order of the Related modifier (sortOrder 110). As a result, your modifier (sortOrder 10) is loaded before the Related modifier declares the Related node config.

To achieve your need, you'll need to set the sortOrder for your modifier to a number higher than 110.

<virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
    <arguments>
        <argument name="modifiers" xsi:type="array">
            <item name="roles-related" xsi:type="array">
                <item name="class" xsi:type="string">YourVendor\YourModule\Ui\DataProvider\Product\Form\Modifier\Related</item>
                <item name="sortOrder" xsi:type="number">120</item>
            </item>
        </argument>
    </arguments>
</virtualType>

Recompile the code (bin/magento setup:di:compile) to see the result.

Note: $meta[static::GROUP_RELATED] does not only contain the config for the Related Products, but also for Up-Sells, and Cross-Sells. Therefore, if you only need to remove the Related Products config, you'll need to use the following code:

unset($meta[static::GROUP_RELATED]['children'][static::DATA_SCOPE_RELATED]);

Check out the modifyMeta method in the Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Related class for reference.

2
  • thank you sir, and can i ask you 1 small question. Can we hide Product Salable Quantity look like Related by this way ?
    – Tom
    Commented Sep 21, 2023 at 4:26
  • @Boll I just saw your new post about this, I have provided the solution in that post magento.stackexchange.com/questions/369838/…
    – Tu Van
    Commented Sep 21, 2023 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.