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: 6 Average: 4.3]
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

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago