2

I need to add a new dropdown field in the "CART PRICE RULE" form in Backend:

MARKETING > PROMOTIONS > CART PRICE RULES > ADD NEW RULE

7

Create file in your custom module app/code/Vendor/Module/view/adminhtml/ui_component/sales_rule_form.xml

<?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="store_ids">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Cms\Ui\Component\Listing\Column\Cms\Options</item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">int</item>
                    <item name="label" xsi:type="string" translate="true">Store View Test</item>
                    <item name="formElement" xsi:type="string">multiselect</item>
                    <item name="source" xsi:type="string">sales_rule</item>
                    <item name="dataScope" xsi:type="string">store_ids</item>
                    <item name="default" xsi:type="string">0</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

And you can see changes (the Store View Test column):

Example

Change it to the desired one. But keep in mind, that this field just rendered in the layout, but to do save and load your custom data you should write a custom plugin code which can do this during the model load and save process.

| improve this answer | |
  • @Sri I'm glad to help you :) – Siarhey Uchukhlebau Feb 27 '18 at 13:33
  • How do I save this field in DB and which file is responsibe to save my custom field – Jaisa Feb 27 '18 at 13:33
  • @Sri You can write an observer for the sales_rule model save\load or plugin. Value can be stored in the same table, or in your own table. If you ask a new question with desired result and requirements, I'll try to help you. – Siarhey Uchukhlebau Feb 27 '18 at 13:35
1

To anyone who needs a complete answer to this question. first, create an installSchema or UpgradeSchema script in your custom module:

class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * Upgrades DB schema for a module
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
       $installer = $setup;
       $installer->startSetup();
        if (version_compare($context->getVersion(), '1.0.3', '<')) {

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

second, create the file

view/adminhtml/ui_component/sales_rule_form.xml

<?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="custom_field" formElement="select">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">custom_field</item>
                </item>
            </argument>
            <settings>
                <dataType>number</dataType>
                <label translate="true">custom field</label>
                <visible>true</visible>
                <dataScope>custom_field</dataScope>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options>
                            <option name="0" xsi:type="array">
                                <item name="value" xsi:type="number">1</item>
                                <item name="label" xsi:type="string" translate="true">Yes</item>
                            </option>
                            <option name="1" xsi:type="array">
                                <item name="value" xsi:type="number">0</item>
                                <item name="label" xsi:type="string" translate="true">No</item>
                            </option>
                        </options>
                    </settings>
                </select>
            </formElements>
        </field>
    </fieldset>
</form>

and that's it you're done

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.