Magento 2.1 – Save Cart Price Rule Custom Fields in Database

adminhtmlmagento-2.1shopping-cart-price-rules

I need to save values of two custom fields which I have added using xml.
Below is the xml in which I have added that fields also they reflect in cart price rule form.

 <fieldset name="custom details">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true"> Detail1</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="string">40</item>
        </item>
    </argument>
    <field name="field1">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">i</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">sales_rule</item>
                <item name="dataScope" xsi:type="string">field1</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
    </field>
    <field name="field2">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">wysiwyg</item>
                <item name="source" xsi:type="string">sales_rule</item>
                <item name="label" xsi:type="string">Field2</item>

                <item name="template" xsi:type="string">ui/form/field</item>
                <item name="wysiwyg" xsi:type="boolean">true</item>
                <item name="dataScope" xsi:type="string">terms_conditions</item>
                <item name="sortOrder" xsi:type="number">50</item>
                <!--<item name="rows" xsi:type="number">8</item>-->
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>

            </item>
        </argument>
    </field>
</fieldset>

I am struggling as on how shall I save the values of two fields in the database against their database field columns.I tried to go through save.php in Namespace/Modulename/Controller/Adminhtml/Promo/Quote/Save.But could find any success in that.
Can someone please help.

Best Answer

You don't need to write custom Model for that, If you add custom field using ui_component than it will save automatically to the DB.

You need to create relative field to salesrule table.

Create setup file at below path.

Vendor/Module/Setup/InstallData.php

public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
) {
    $setup->startSetup();

     $setup->getConnection()->addColumn(
        $setup->getTable('salesrule'),
        'group_members',
        [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'length' => 32,
            'nullable' => false,
            'default' => '',
            'comment' => 'Group Members'
        ]
    );

Than Run below commands from Magento root.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
php bin/magento cache:clean

Note: If you modifying existing Module setup file than you must need to remove your module entry from setup table.

Hope it will help you !!

Related Topic