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
Contents
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
1 2 3 4 5 6 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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!
Do you have guidelines for the implementation of actions? I need your help. Thank you.
For the customization, you need to contact us at https://magecomp.com/contact