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>
-
1how you create this attribute?Dhiren Vasoya– Dhiren Vasoya2021年11月18日 10:56:56 +00:00Commented Nov 18, 2021 at 10:56
-
add this product_form.xml file in my module and its are visible in adminside but value not saveNoOn– NoOn2021年11月18日 10:58:51 +00:00Commented Nov 18, 2021 at 10:58
-
1You need to just create the product attribute using magento methods only and assign that attributeset, rest things is manage by magneto it self.Dhiren Vasoya– Dhiren Vasoya2021年11月18日 11:00:47 +00:00Commented Nov 18, 2021 at 11:00
-
can you sow me working example? or something like that?NoOn– NoOn2021年11月18日 11:01:53 +00:00Commented Nov 18, 2021 at 11:01
2 Answers 2
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.
should be hidden or visible? can you leave a more complete code for creating product_form.xml fields through and creating and saving an attribute? My code which doesn't save or output attribute
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="product_duo" sortOrder="6">
<settings>
<label translate="true">Duo</label>
<collapsible>true</collapsible>
<opened>true</opened>
<dataScope>product_duo</dataScope>
</settings>
<field name="duo_product" formElement="input" sortOrder="10">
<settings>
<visible>true</visible>
<elementTmpl>ui/form/element/input</elementTmpl>
<label translate="true">Duo product</label>
<dataScope>duo_product</dataScope>
<imports>
<link name="linkedValue">${$.provider}:data.product.duo_product</link>
</imports>
</settings>
</field>
</fieldset>
</form>
$eavSetup->addAttribute(
Product::ENTITY,
'duo_product',
[
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => __('Duo product'),
'input' => 'hidden',
'class' => '',
'source' => '',
'global' => ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'apply_to' => '',
'is_configurable' => false,
]