Save Custom Attribute Value from product_form.xml – Magento2 Guide

adminhtmlmagento2

Add new attribute in product create page in admin side

How can I save value from this dropdown?

here is my product_form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="product-details">
        <field name="featured_category_ids" sortOrder="100" formElement="select" component="Magento_Ui/js/form/element/ui-select">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filterOptions" xsi:type="boolean">true</item>
                    <item name="showCheckbox" xsi:type="boolean">true</item>
                    <item name="disableLabel" xsi:type="boolean">true</item>
                    <item name="multiple" xsi:type="boolean">true</item>
                    <item name="levelsVisibility" xsi:type="number">1</item>
                    <item name="listens" xsi:type="array">
                        <item name="${ $.namespace }.${ $.namespace }:responseData" xsi:type="string">setParsed</item>
                    </item>
                </item>
            </argument>
            <settings>
                <label translate="true">Featured Categories</label>
                <dataType>text</dataType>
                <elementTmpl>ui/grid/filters/elements/ui-select</elementTmpl>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options class="Magento\Catalog\Ui\Component\Product\Form\Categories\Options"/>
                    </settings>
                </select>
            </formElements>
        </field>
    </fieldset>
</form>

Best Answer

You will need to create a product attribute and add it to the product's attribute set first.

Then, reference the attribute ID in your field dataScope like so:

        <field name="attribute_id" formElement="select">
            <settings>
                <dataScope>attribute_id</dataScope>
                <imports>
                    <link name="linkedValue">${$.provider}:data.product.attribute_id</link>
                </imports>
            </settings>
        </field>

If the two IDs match, Magento will save the value.