cancel
Showing results for 
Search instead for 
Did you mean: 

Add custom Product field and save it

Add custom Product field and save it

Using the magento2 devdocs (http://devdocs.magento.com/guides/v2.1/howdoi/customize_product.html) I managed to add a new fieldset and field to the product form. But where I am stuck now is how to save the new value to the database. I have been googling for the last two days and I cannot come up with a solution. How would I do this? Any help is appreciated, thank you. 

2 REPLIES 2

Re: Add custom Product field and save it

Hi @Ophi,

 


Could you please share the code that you implemented to see if there is something missing?


Best regards.
Gabriel

Welcome to the Magento Forums. Remember to introduce yourself and read the Magento Forums Guidelines.

Re: Add custom Product field and save it

Hi, 

 

my product_form.xml looks like this: 

 

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
  <fieldset name="eintest">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Custom Stuff</item>
            <item name="sortOrder" xsi:type="number">1</item>
        </item>
    </argument>
        <field name="brand">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="value" xsi:type="number">0</item>
                </item>
                <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\Something</item>
            </argument>
        </field>
  </fieldset>
</form>

and the Something.php class looks like this: 

 

class Something implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * Options getter
     *
     * @return array
     */
    public function toOptionArray()
    {
        return [['value' => 1, 'label' => __('M2')], ['value' => 0, 'label' => __('M1')]];
    }

    /**
     * Get options in "key-value" format
     *
     * @return array
     */
    public function toArray()
    {
        return [0 => __('M1'), 1 => __('M2')];
    }
}

Thank you!