5

In magento 2 how to add custom options in action apply field in cart price rule form in admin

In admin panel -> marketing ->cart price rule -> add new rule

Image:

enter image description here

Reference Image

enter image description here

1
  • Thanks @DivyaSekar. Happy to help !! Happy coding. Okay no problem. +1 for acceptance :) Aug 12, 2019 at 7:12

2 Answers 2

14
+50

This Dropdown options render from getMetadataValues() function which is available in Magento\SalesRule\Model\Rule\Metadata\ValueProvider.php

So, You can create plugin for that because, it's public method. I created plugin for that and get output as like you want.

Follow this below steps for that :

1) Create di.xml to set plugin at app/code/RH/CustomPlugin/etc/adminhtml :

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\SalesRule\Model\Rule\Metadata\ValueProvider">
      <plugin name="salesrule-plugin" type="RH\CustomPlugin\Plugin\Rule\Metadata\ValueProvider" sortOrder="1" />
    </type>
</config>

2) Now, create plugin file ValueProvider.php for add custom option for dropdown at RH\CustomPlugin\Plugin\Rule\Metadata:

<?php

namespace RH\CustomPlugin\Plugin\Rule\Metadata;

class ValueProvider {
    public function afterGetMetadataValues(
        \Magento\SalesRule\Model\Rule\Metadata\ValueProvider $subject,
        $result
    ) {
        $applyOptions = [
            'label' => __('Popular'),
            'value' => [
                [
                    'label' => 'The Cheapest, also for Buy 1 get 1 free',
                    'value' => 'buy-1-get-1-free',
                ],
                [
                    'label' => 'Get $Y for each $X spent',
                    'value' => 'get-y-for-each-x-spent',
                ],
            ],
        ];
        array_push($result['actions']['children']['simple_action']['arguments']['data']['config']['options'], $applyOptions);
        return $result;
    }
}

Output :

enter image description here

You can modify option in array as you want.

Hope, it will helpful for you.

9
  • 1
    Thanks its working what i expect i will award the bounty after 21 hours @Rohan prnt.sc/orh99g Aug 12, 2019 at 7:12
  • 1
    Thank you @divyasekar :) Aug 13, 2019 at 6:03
  • how to create a dependency field for custom options @Rohan Hapani can u please help me Aug 17, 2019 at 8:05
  • 1
    Have you got the solution? After adding those option did the discount applied?
    – aravind
    Oct 9, 2019 at 7:13
  • 1
    It's work. Thank you Rohan for your dedication. Mar 2 at 6:29
1

Above answer does not works! You need to make below corrections in above code.

$applyOptions = [
        [
            'label' => __('Gift with purchase (order level)'),
            'value' => 'gift-with-purchase-order-level',
        ],
        [
            'label' => __('Gift with purchase (item level)'),
            'value' => 'gift-with-purchase-item-level',
        ],
        [
            'label' => __('New Order Creation (free)'),
            'value' => 'new-order-creation-free',
        ],
        [
            'label' => __('New Order Creation (discount)'),
            'value' => 'new-order-creation-discount',
        ],
    ];
    foreach($applyOptions as $optionArr) {
       array_push($result['actions']['children']['simple_action']['arguments']['data']['config']['options'], $optionArr);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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