Custom Fields Product Add Edit Pages Magento 2

This is a quite important demand when you want to use extra information on your pages which the default system does not include.

You can add custom fields with the help of UI Component.

Follow the below steps to create the module ‘PHPCodez_Customfield’

1) Declare a module.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PHPCodez_Customfield" setup_version="0.0.1" />
</config>

2) Register a module.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'PHPCodez_Customfield',
__DIR__
);

3) Generate UI Component

<?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"> 
<fieldset name="product_extra">
<argument name="data" xsi:type="array"> 
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Product Extra Information</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">false</item>
</item>
</argument>
<field name="product_label">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Label</item>
<item name="formElement" xsi:type="string">input</item>
<item name="sortOrder" xsi:type="number">1</item>
<item name="scopeLabel" xsi:type="string">[GLOBAL]</item>
</item>
</argument>
</field> 
</fieldset>
</form>

4) Issue the following commands.

$ php bin/magento indexer:reindex
$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento cache:flush

One thought on “Custom Fields Product Add Edit Pages Magento 2”

Leave a Reply

Your email address will not be published. Required fields are marked *