Hi I want to add a custom field in Product add/Edit form using UiComponent and want to save its value and displayed back to form as well
I have read a couple of threads for this – but not getting the exact solution
I have added custom field using uicomponent in product_form.xml file below is the code :
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<modal name="advanced_inventory_modal">
<fieldset name="manage_note">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Manage Note</item>
<item name="dataScope" xsi:type="string"/>
<item name="sortOrder" xsi:type="number">0</item>
<item name="collapsible" xsi:type="boolean">true</item>
<item name="opened" xsi:type="boolean">true</item>
</item>
</argument>
<field name="note">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Notes</item>
<item name="formElement" xsi:type="string">textarea</item>
<item name="dataScope" xsi:type="string">quantity_and_stock_status.note</item>
<item name="sortOrder" xsi:type="number">1</item>
<item name="scopeLabel" xsi:type="string">[GLOBAL]</item>
</item>
</argument>
</field>
</fieldset>
<fieldset name="stock_data">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Stock Configuration</item>
<item name="dataScope" xsi:type="string"/>
<item name="sortOrder" xsi:type="number">100</item>
<item name="collapsible" xsi:type="boolean">true</item>
</item>
</argument>
</fieldset>
</modal>
</form>
Custom Field is added successfully, but its value is not getting saved ? due to that its value is not displayed in edit form as well.
so what is the best approach to achieve this?
Best Answer
I have resolved this issue by myself, below is the way i have resolved it.
I have created Observer which calls after product save event.
so below is the events.xml file below is the code :
Below is my Observer AfterProductSave code :
Updated Date:- 10th Aug, 2021
if you can't show value of custom field at Edit Page then try below code
In above Modifier File CustomField.php there are two functions,
1st one modifyData() - adding data when the product edit page is loaded.
2nd one modifyMeta() – creating field, fieldset. it is used for create custom field. but it's not necessary to use modifyMeta() for create Custom field, Fieldset in form, If you are using Magento 2 Or above Version then you can add/create field, fieldset using .xml file UI Component