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:
- Create UpgradeSchema.php file at app\code\Meetanshi\Mtapi\Setup\
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950<?phpnamespace 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();}}
123456789101112131415161718<?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.
Any doubts? Do mention them in the Comments section below.
I would be glad to help you out.
Also, please share the solution with Magento Community via social media.
Thank you.
Related Post
- How To Add Magento Shopping Cart Price Rules
- How to Get All Coupon Codes of Particular Cart Price Rules in Magento 2
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
How to Install Magento 2.4.2 on Localhost Using Xampp
How to Join 2 Tables in Magento 2
Next