How To

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.

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!

Click to rate this post!
[Total: 7 Average: 4.4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago