How To

How to Add Custom Field in Catalog Price Rule Form in Magento 2?

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!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Congratulations on creating such a useful website. Not only is your blog helpful, but it's also incredibly imaginative.

  • Magnificent and intriguing article. Incredible things you've generally imparted to us. Much appreciated. Simply keep making this sort out of post.

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

1 day ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago