I am using REST API and want to validate coupon code, whether it is applicable or not.
I have no checkout/session and no quote. I just have cart data and user checkout information.
What I actually want to achieve is, whenever I apply coupon code, it should only accept if it passes all the validations, all the conditions, and all the action rules defined inside the shopping cart price rule.
What is the proper way to validate the coupon code against all its rules ?
How should this be implemented ?
What are the alternatives ?
Best Answer
Finally resolved by myself, using Magento 1.9
First of all Magento V2 API for adding a coupon code on cart does not provide complete validations
e.g. Coupon Dates, Uses Per Coupon, Coupon usage and Customer Group.
Above mentioned point is very important to notice. If you call the coupon code add API, then the API call will surpass these constraints and will not validate or restrict you. Magento V2 API for Coupon codes only validates the Conditions and Action rules but does not validate the Coupon.
e.g. Cart subtotal validations, cart products by category validations and so on.
In the solution of all of above I have solved it using MAgento 1.9 V2 API + Custom API.
I have created custom API to validate if the coupon is applicable or not and this is my first step as well.
e.g. Coupon applicable Date range, User per customer, Coupon usage limit and Customer Group validations
I have used below function in custom API to validate coupon constraints (you can change the business logic according to yours) :
If the above function responds with success then we can proceed with the simple Magento SOAP V2 APIs to apply Discount code
Following is the working example script to apply Discount Code using Magento V2 APIs :
Note: Must notice that I have provided Store ID in every API call this is very important. Else your discount code will throw an "Invalid coupon" error every time.
That's it. I hope this might help someone facing problems. Enjoy !