How To

How to Add a Custom Field to the Cart Price Rules Form in Magento 2?

Hello Magento Friends,

In this blog, we will learn How to Add a Custom Field to the Cart Price Rules Form in Magento 2.

Cart price rules are created by the admin to apply discounts on shopping cart items based on certain conditions. Magento allows adding customization to the cart price rule creation. When you want additional rule parameters or custom behavior of cart discounts, you need a custom field in the cart price rules.

Follow the below steps to add a custom field to the cart price rules form in Magento 2.

Steps to Add a Custom Field to the Cart Price Rules Form in Magento 2:

Step 1: Create the “db_schema.xml” file inside our extension at the following path.

app\code\Vendor\Extension\etc\db_schema.xml

Now add the code as follows

<?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="salesrule" resource="default">
        <column xsi:type="smallint" name="enable_customfield" unsigned="false" nullable="false" identity="false"
                default="0" comment="Is Enable Customfield"/>
      </table>
</schema>

Step 2: Create the “sales_rule_form.xml” file inside our extension at the following path.

app\code\Vendor\Extension\view\adminhtml\ui_component\sales_rule_form.xml

Then add the following 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">
    <fieldset name="rule_information" sortOrder="10">
        <field name="enable_customfield" formElement="checkbox">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">sales_rule</item>
                    <item name="default" xsi:type="number">0</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">false</rule>
                </validation>
                <dataType>boolean</dataType>
                <label translate="true">Enable Custom field</label>
                <dataScope>enable_customfield</dataScope>
            </settings>
            <formElements>
                <checkbox>
                    <settings>
                        <valueMap>
                            <map name="false" xsi:type="number">0</map>
                            <map name="true" xsi:type="number">1</map>
                        </valueMap>
                        <prefer>toggle</prefer>
                    </settings>
                </checkbox>
            </formElements>
        </field>
    </fieldset>
</form>

Output:

Conclusion:

Hence, following the above steps you can customize the cart price rules form in Magento 2 by adding a custom field. In case you face any difficulty, share it with me through the comment section and I will be quick to provide you with the solution. Moreover, share the solution with your friends and stay in touch with us for more such Magento 2 customization solutions.

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

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.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago