How to create a customization cart price rule condition in Magento 2
Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series.
Today, I am going to show everybody the best practice, How to create a customization cart price rule condition in Magento 2.
As far as I know, by default Magento provide us a feature called “Cart Price Rules”. You can find it in the Admin panel → Marketing → Promotions → Cart Price Rules. You can define many different options in which a given discount should be applied in the cart. However, the default options don’t cover all possible situations.
In some case, you want to add the customization cart price rule via your condition, but you don’t know how to add it by programmatically. This practice will help you steps by step to do it works.
We have the following scenario: We will add a new cart price rule, that if the customer has gender is Male, the coupon code should be applied.
Before doing this practice, you must understand, how to use Event and Observer in Magento 2. If you don’t learn this practice yet, you can watch it here:
In this practice, we will use the event named “salesrule_rule_condition_combine” for adding the new customization condition.
The main part, you will get after completing this practice:
1. You will know, how to create a new module in Magento 2.
2. You will know, how to use Event and Observer in Magento 2.
Okie, let’s go.
Let’s do this practice, you need to follow steps by step:
Step 1: Declaring a new module called PHPCuong_CustomerCondition
– Create the namespace PHPCuong in the path appcode
– Create the module named CustomerCondition in the path appcodePHPCuong
– Create the file named registration.php in the path appcodePHPCuongCustomerCondition
– Create the file named module.xml in the path appcodePHPCuongCustomerConditionetc
End of the step #1, I have been completed the codes to create a new module called PHPCuong_CustomerCondition.
Step 2: Subscribing to the event named salesrule_rule_condition_combine
– We will create an observer that will add our condition
– Create the new file named events.xml in the path appcodePHPCuongCustomerConditionetc
– Create the new file named CustomerConditionObserver.php in the path appcodePHPCuongCustomerConditionObserver
– Create the new file named di.xml in the path appcodePHPCuongCustomerConditionetc
– Create the new file named Gender.php in the path appcodePHPCuongCustomerConditionModelRuleConditionCustomer
Step 3: Test and see the results
– Run the following command lines:
php bin/magento setup:upgrade –keep-generated
The new module will be added into the table named setup_module.
php bin/magento cache:flush
– Go to the Admin Panel → Customers → Add New Customer.
My server can not send an email now so I can not get an access to the new customer account. I will try to login with the old customer account available.
– Go to the storefront, log in with the new customer account.
– Go to the Admin Panel → Marketing → Promotions → Cart Price Rules → Add New Rule
– Go to the storefront, add the product to the shopping cart and go to the checkout page, then applying the new coupon code.
Yeah, It works perfectly, for now, I will try to change the gender of this customer to Female and try to apply the coupon code again.
Thank you for your watching. If you have any questions about this practice please feel free to leave a comment below.
Don’t forget to like, comment, share and subscribe to my channel for getting the latest videos.
Please do not hesitate to contact me if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2.
Get the source codes of this practice in the Github