Create a Cart Price Rule programmatically via Magento 2’s REST API

1. API Endpoint

Use the following endpoint to create a cart price rule:

bash

Copy code
POST /V1/salesRules

2. Request Payload

Here’s an example of the JSON payload to create a cart price rule:

json

Copy code
{
  "rule": {
    "name": "Buy 3 Get Discount",
    "description": "Discount applied when purchasing 3 specific items",
    "is_active": true,
    "stop_rules_processing": false,
    "website_ids": [1],
    "customer_group_ids": [0, 1, 2],
    "coupon_type": 1,
    "discount_qty": null,
    "apply_to_shipping": false,
    "times_used": 0,
    "is_rss": false,
    "use_auto_generation": false,
    "simple_action": "by_percent",
    "discount_amount": 10,
    "conditions": {
      "type": "MagentoSalesRuleModelRuleConditionCombine",
      "aggregator": "all",
      "value": "1",
      "conditions": [
        {
          "type": "MagentoSalesRuleModelRuleConditionProduct",
          "attribute": "sku",
          "operator": "==",
          "value": "SKU123"
        }
      ]
    },
    "actions": {
      "type": "MagentoSalesRuleModelRuleConditionProduct",
      "attribute": "qty",
      "operator": ">=",
      "value": "3"
    }
  }
}

Explanation of Key Fields:

  • name: The name of the rule.
  • description: An optional description for internal reference.
  • is_active: Set to true to activate the rule.
  • website_ids: IDs of the websites where the rule is applicable.
  • customer_group_ids: IDs of customer groups eligible for the rule.
  • simple_action: Discount type. Options include:
  • by_percent: Percentage discount.
  • by_fixed: Fixed discount per item.
  • discount_amount: The amount of the discount.
  • conditions: Defines when the rule is applied (e.g., based on product SKU or quantity).
  • actions: Defines what happens when the rule is triggered.

3. cURL Example

Here’s a cURL example to create the rule:

bash

Copy code
curl -X POST "https://your-magento-instance.com/rest/V1/salesRules" 
-H "Content-Type: application/json" 
-H "Authorization: Bearer <access-token>" 
-d '{
  "rule": {
    "name": "Buy 3 Get Discount",
    "description": "Discount applied when purchasing 3 specific items",
    "is_active": true,
    "stop_rules_processing": false,
    "website_ids": [1],
    "customer_group_ids": [0, 1, 2],
    "coupon_type": 1,
    "discount_qty": null,
    "apply_to_shipping": false,
    "times_used": 0,
    "is_rss": false,
    "use_auto_generation": false,
    "simple_action": "by_percent",
    "discount_amount": 10,
    "conditions": {
      "type": "MagentoSalesRuleModelRuleConditionCombine",
      "aggregator": "all",
      "value": "1",
      "conditions": [
        {
          "type": "MagentoSalesRuleModelRuleConditionProduct",
          "attribute": "sku",
          "operator": "==",
          "value": "SKU123"
        }
      ]
    },
    "actions": {
      "type": "MagentoSalesRuleModelRuleConditionProduct",
      "attribute": "qty",
      "operator": ">=",
      "value": "3"
    }
  }
}'

Replace:

  • <access-token>: With your admin access token.
  • your-magento-instance.com: With your Magento instance URL.
  • SKU123: With the SKU of the product.

Leave a comment

Your email address will not be published. Required fields are marked *