How to Add Custom Options in Action Apply Field of Cart Price Rule Form in Magento 2

How to Add Custom Options in Action Apply Field of Cart Price Rule Form in Magento 2

Hello Magento Friends,

Customization is the centerpiece of the Magento framework. In this tutorial, I am going to explain How to Add Custom Options for the Action field of the Cart Price Rule Form in Magento 2.

Cart Price Rules are created when you want to offer any discount to customers. Admin needs to create cart price rules and apply the conditions and actions from the Magento backend.

Note: Track all the activities performed by your admin users by installing Magento 2 Admin Action Log.

Magento 2 provides four default actions for the apply field in the cart price rule.

cart price rule action

However, if the default options don’t match your requirements, you can add your custom options too in the actions apply field of cart price rule in Magento 2. Let’s check how

Steps to Add Custom Options in Action Apply Field of Cart Price Rule Form in Magento 2:

Step 1: Add di.xml file for the plugin in the following file path

app\code\Vendor\Extension\etc\adminhtml

Now, add the code as follows

<?xml version="1.0" encoding="UTF-8"?>
<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="salesactionrule-plugin" type="Vendor\Extension\Plugin\Rule\Metadata\Valueprovider" sortOrder="1" /> 
   </type> 
</config>

Step 2: Create plugin file Valueprovider.php in the below file path to add custom option in action field dropdown

app\code\Vendor\Extension\Plugin\Rule\Metadata

Then add the below-mentioned piece of code

<?php 

namespace app\code\Vendor\Extension\Plugin\Rule\Metadata; 

class Valueprovider
{ 
    public function afterGetMetadataValues( 
           \Magento\SalesRule\Model\Rule\Metadata\ValueProvider $subject, 
            $result )
    { 
        $applyOptions = [ 
        'label' => __(Action Title), 
        'value' => [ 
            [ 'label' => 'Action field label 1', 'value' => 'Action field value 1', ], 
            [ 'label' => 'Action field label 2', 'value' => 'Action field value 2', ], 
         ], 
         ]; 
         array_push($result['actions']['children']['simple_action']['arguments']['data']['config']['options'], $applyOptions);
         return $result;
    }
}

Conclusion:

Accordingly, you can add custom options to apply field dropdown in the cart price rule actions tab of Magento 2. In case you are unable to add, feel free to reach me through the comments. Share the article with the ones who are in search of cart price rule customization.

Happy Coding!

Previous Article

6 Financial Steps to Taking Your eCommerce Site to a New Level

Next Article

Magento 2 Extensions Digest February 2022 (New Release & Updates)

Write a Comment
  1. Do you have guidelines for the implementation of actions? I need your help. Thank you.

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨