How to Add New Field in Cart Price Rules Form in Magento 2 Backend

Magento 2 CMS is popular among store merchants due to its capability of customization to satisfy custom business requirements.

Today, I will talk about one such customization to add new field in cart price rules form in Magento 2 backend.

The admin can use the below programmatic solution to add a new custom field in the cart price rules form in the admin panel of Magento 2.

For instance, while using any third-party APIs for offers, you need to pass any data and for that, you require adding a custom field.

Any such requirements can be done using the below method:

Method to Add New Field in Cart Price Rules Form in Magento 2 Backend:

1. Create UpgradeSchema.php file at app\code\Meetanshi\Mtapi\Setup\

<?php

namespace Meetanshi\Mtapi\Setup;

use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    protected $resourceConfig;

    public function __construct(
        ConfigInterface $resourceConfig)
    {
        $this->resourceConfig = $resourceConfig;
    }

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        $installer->getConnection()->addColumn(
            $installer->getTable('salesrule'),
            'redirection_link',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'unsigned' => true,
                'nullable' => true,
                'default' => '',
                'comment' => 'redirection_link'
            ]
        );

        $installer->getConnection()->addColumn(
            $installer->getTable('salesrule'),
            'offer_image',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'unsigned' => true,
                'nullable' => true,
                'default' => '',
                'comment' => 'offer_image'
            ]
        );
        $installer->endSetup();
    }
}

2. Create sales_rule_form.xml file at app\code\Meetanshi\Mtapi\view\adminhtml\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="rule_information" sortOrder="10">
        <field name="redirection_link" 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>
                <dataType>text</dataType>
                <label translate="true">Redirection Link</label>
                <dataScope>redirection_link</dataScope>
            </settings>
        </field>
    </fieldset>
</form>

That’s it.

Also, please share the solution with Magento Community via social media.

Thank you.

Table of Contents