Magento 2 API – Create Cart Price Rule
Magento Integration Made Easy!
Integrate any third-party tool or service with your store using our Magento API Integration Services.
Hello Magento Peeps! 😀
I am back with another blog post on the Magento 2 API. Today, I will show you how to create a cart price rule in Magento 2 using REST API.
Offering lucrative deals and discounts to customers is one of the common ways online stores use to increase conversions, average order value, and ultimately sales. By default, Magento 2 allows the merchants to create different rules for offering discounts, known as cart price rules. The Magento 2 cart price rules API allows the client to create and manage cart price rules in Magento 2 through the REST API.
If you are developing a new admin panel, then you need to call the Magento 2 APIs for implementing the admin-level functions in it – such as creating cart price. In this blog post, I will show how you can create a cart price rule in Magento 2 using API.
Let’s begin…!
How to Create Cart Price Rule in Magento 2 Using REST API?
One thing to note here is that creating a cart price rule requires admin access in Magento 2. Therefore, to create a cart price rule in Magento 2 using API, the client needs to pass the admin access token in the header of the request. Learn more about Magento 2 admin access token.
In order to create a cart price rule using API in Magento 2, the client needs to make a POST request to the salesRules endpoint with all the required details in the body. Let’s understand it better using an example.
Method: POST
URL: store_url/rest/V1/salesRules
Headers:
- Content Type: application/json
- Authorization: Bearer <admin token>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
{ "rule": { "name": "Test API", "website_ids": [ 1 ], "customer_group_ids": [ 0,1,2,3 ], "uses_per_customer": 0, "is_active": true, "stop_rules_processing": false, "is_advanced": true, "sort_order": 10, "discount_amount": 1000000, "discount_step": 1, "apply_to_shipping": false, "times_used": 0, "is_rss": true, "coupon_type": "NO_COUPON", "use_auto_generation": false, "uses_per_coupon": 0 } } |
Response:
On successful creation of the cart price rule, Magento 2 will return the rule_id in the API response.
Modify Cart Price Rule in Magento 2 Using REST API
The create card price rule Magento 2 API can also be used to modify the existing cart price rules. All you need to do is just pass the rule_id in the body of the request along with other parameters. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
{ "rule": { "rule_id": 3, "name": "Test API", "website_ids": [ 1 ], "customer_group_ids": [ 0,1,2,3 ], "uses_per_customer": 0, "is_active": true, "stop_rules_processing": false, "is_advanced": true, "sort_order": 10, "discount_amount": 1000000, "discount_step": 1, "apply_to_shipping": false, "times_used": 0, "is_rss": true, "coupon_type": "NO_COUPON", "use_auto_generation": false, "uses_per_coupon": 0 } } |
That’s it!
This is how you can create and modify cart price rules in Magento 2 using API.
In case you have any doubts or queries regarding the above solution, feel free to comment. I will be happy to help you. 😊
Also, do not forget to check out our other Magento 2 API blog posts.
Thanks for reading! 🍀
◄ Magento 2 API – Create an InvoiceMagento 2 API – Create Coupon Code ►
Connect your Magento store with any third-party tool or service & improve your business efficiency.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
Things to Consider When Hiring Magento Developer [2024]
Magento 2 API – Create Coupon Code
Next