{"id":2103,"date":"2022-09-09T06:58:24","date_gmt":"2022-09-09T06:58:24","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/magento-2-api-create-cart-price-rule\/"},"modified":"2025-03-17T08:53:33","modified_gmt":"2025-03-17T08:53:33","slug":"magento-2-api-create-cart-price-rule","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/magento-2-api-create-cart-price-rule\/","title":{"rendered":"Magento 2 API &#8211; Create Cart Price Rule"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello Magento Peeps!  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I am back with another blog post on the&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API<\/a>. Today, I will show you how to create a cart price rule in Magento 2 using REST API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;<strong>cart price rules<\/strong>. The Magento 2 cart price rules API allows the client to create and manage cart price rules in Magento 2 through the REST API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 \u2013 such as creating cart price. In this blog post, I will show how you can&nbsp;<em><strong>create a cart price rule in Magento 2 using API<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s begin\u2026!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create Cart Price Rule in Magento 2 Using REST API?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-admin-token\/\" target=\"_blank\" rel=\"noreferrer noopener\">Learn more about Magento 2 admin access token<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to create a cart price rule using API in Magento 2, the client needs to make a POST request to the&nbsp;<em>salesRules&nbsp;<\/em>endpoint with all the required details in the body. Let\u2019s understand it better using an example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Method:<\/strong>&nbsp;POST<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>URL:<\/strong>&nbsp;store_url\/rest\/V1\/salesRules<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Headers:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Content Type:<\/strong>&nbsp;application\/json<\/li>\n\n\n\n<li><strong>Authorization:<\/strong>&nbsp;Bearer &lt;admin token&gt;<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"rule\": {\n    \"name\": \"Test API\",\n    \"website_ids\": [\n      1\n    ],\n    \"customer_group_ids\": [\n      0,1,2,3\n    ],\n    \"uses_per_customer\": 0,\n    \"is_active\": true,\n    \"stop_rules_processing\": false,\n    \"is_advanced\": true,\n    \"sort_order\": 10,\n    \"discount_amount\": 1000000,\n    \"discount_step\": 1,\n    \"apply_to_shipping\": false,\n    \"times_used\": 0,\n    \"is_rss\": true,\n    \"coupon_type\": \"NO_COUPON\",\n    \"use_auto_generation\": false,\n    \"uses_per_coupon\": 0\n  }\n}<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2022\/09\/Create-Cart-Price-Rule-in-Magento-2.png\" alt=\"Magento 2 API Request to Create a Cart Price Rule\" class=\"wp-image-51489\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Response:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On successful creation of the cart price rule, Magento 2 will return the rule_id in the API response.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Modify Cart Price Rule in Magento 2 Using REST API<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;<strong>rule_id<\/strong>&nbsp;in the body of the request along with other parameters. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"rule\": {\n   \"rule_id\": 3,\n    \"name\": \"Test API\",\n    \"website_ids\": [\n      1\n    ],\n    \"customer_group_ids\": [\n      0,1,2,3\n    ],\n    \"uses_per_customer\": 0,\n    \"is_active\": true,\n    \"stop_rules_processing\": false,\n    \"is_advanced\": true,\n    \"sort_order\": 10,\n    \"discount_amount\": 1000000,\n    \"discount_step\": 1,\n    \"apply_to_shipping\": false,\n    \"times_used\": 0,\n    \"is_rss\": true,\n    \"coupon_type\": \"NO_COUPON\",\n    \"use_auto_generation\": false,\n    \"uses_per_coupon\": 0\n  }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is how you can create and modify cart price rules in Magento 2 using API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case you have any doubts or queries regarding the above solution, feel free to comment. I will be happy to help you.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do not forget to check out our other&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API<\/a>&nbsp;blog posts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading!  <\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button btn-orange\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-create-an-invoice\/\">Magento 2 API \u2013 Create an Invoice<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button btn-orange\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-create-coupon-code\/\">Magento 2 API \u2013 Create Coupon Code<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello Magento Peeps! I am back with another blog post on the&nbsp;Magento 2 API. Today, I will show you how to create a cart price&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2103","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2103"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2103\/revisions"}],"predecessor-version":[{"id":16977,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2103\/revisions\/16977"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}