2

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

  • Thanks @DivyaSekar. Happy to help !! Happy coding. Okay no problem. +1 for acceptance :) – Rohan Hapani Aug 12 '19 at 7:12
4
+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.

  • 1
    Thanks its working what i expect i will award the bounty after 21 hours @Rohan prnt.sc/orh99g – divya sekar Aug 12 '19 at 7:12
  • 1
    Thank you @divyasekar :) – Rohan Hapani Aug 13 '19 at 6:03
  • how to create a dependency field for custom options @Rohan Hapani can u please help me – divya sekar Aug 17 '19 at 8:05
  • can you please help me how to use the custom option action in frontend (cart page) – divya sekar Aug 26 '19 at 12:36
  • Yeah sure. Let me check that. – Rohan Hapani Aug 26 '19 at 12:40

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.