Hello Magento Friends,
Today’s guide is about How to Add Custom Field in Catalog Price Rule Form in Magento 2?
Catalog price rules are created when merchants want to offer discounts on store products or services. Store owners set rules that must be fulfilled to avail discounts.
Learn – How to Create a Catalog Price Rule in Magento 2
While creating catalog price rules from the Magento 2 backend, the admin needs to apply the required configuration settings. But if you want a custom field in creating catalog price rules, refer to the below steps.
Steps to Add Custom Field in Catalog price Rule Form in Magento 2:
Step 1: Add a custom field in the catalog rule table with db_schema.xml. Navigate to the following path
app/code/Vendor/Extension/etc/db_schema.xml
Now, add the below code
<?xml version="1.0"?> <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd"> <table name="catalogrule"> <column xsi:type="varchar" name="custom_field" nullable="false" length="255" comment="Custom Field Catalog Price Rule" /> </table> </schema>
Step 2: Create catalog_rule_form.xml file inside ui_component. Go to the below path
app/code/Vendor/Extension/view/adminhtml/ui_component/catalog_rule_form.xml
Then, add the code as follows
<?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="rule_information" sortOrder="10"> <field name="custom_field" formElement="input"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="source" xsi:type="string">sales_rule</item> </item> </argument> <settings> <validation> <rule name="required-entry" xsi:type="boolean">false</rule> </validation> <dataType>text</dataType> <label translate="true">Custom Field</label> <visible>true</visible> <dataScope>custom_field</dataScope> </settings> </field> </fieldset> </form>
Conclusion:
Hence, this way you can add any custom field to the catalog price rule form in Magento 2. If you are unable to add a custom field to the catalog price rule, let me know through the comment section. Share the article with your friends and stay updated with us for more solutions.
Happy Coding!