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.
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:
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!
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…