0

I am currently able to hide, disable or set default values for fields on product creation form via the following: 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="testAttribute" xsi:type="array">
                    <item name="class" xsi:type="string">Mypackage\Custom\Ui\DataProvider\Product\Form\Modifier\Attributes</item>
                    <item name="sortOrder" xsi:type="number">1000</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

Ui/DataProvider/Product/Form/Modifier/Attributes.php

<?php

namespace Mypackage\Custom\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Catalog\Model\Locator\LocatorInterface;

class Attributes extends AbstractModifier
{
    private $arrayManager;
    private $locator;

    public function __construct(
        ArrayManager $arrayManager ,LocatorInterface $locator

    ) {
        $this->arrayManager = $arrayManager;
                $this->locator = $locator;
    }

    public function modifyData(array $data)
    {
        //var_dump($this);
        //exit;

        $model = $this->locator->getProduct();
        $modelId = $model->getId();

        if (!isset($data[$modelId][self::DATA_SOURCE_DEFAULT]['quantity_and_stock_status']['qty'])) {
            $data[$modelId][self::DATA_SOURCE_DEFAULT]['quantity_and_stock_status']['qty'] = '1';
        }
         $data[$modelId][self::DATA_SOURCE_DEFAULT]['attribute_set_id'] = '9';

        return $data;
      }
}

Everything works even the Attribute Set dropdown shows the name of the said attribute but the form doesn't pull ajax fields of the assigned attribute_set_id.I have to toggle the dropdown to default and back to module set value to show the fields of the attribute set.

Is this not the correct approach of selecting a different default attribute_set_id ?

1 Answer 1

1

Please try below solution once:

Filename: 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">
    <type name="Magento\Catalog\Ui\DataProvider\Product\Form\ProductDataProvider">
        <arguments>
            <argument name="meta" xsi:type="array">
                <item name="product-details" xsi:type="array">
                    <item name="children" xsi:type="array">
                        <item name="attribute_set_id" xsi:type="array">
                            <item name="arguments" xsi:type="array">
                                <item name="data" xsi:type="array">
                                    <item name="config" xsi:type="array">
                                        <item name="default" xsi:type="string">4</item>
                                        <!-- Change this to your desired attribute_set_id -->
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </item>
            </argument>
        </arguments>
    </type>
</config>
1
  • that works, thanks.
    – raza19
    Feb 19 at 15:40

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.