Back to Woocommerce

Coupons

docs/apis/rest-api/v1/coupons.mdx

10.9.0-dev25.8 KB
Original Source

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Coupons

The coupons API allows you to create, view, update, and delete individual, or a batch, of coupon codes.

Coupon properties

AttributeTypeDescription
idintegerUnique identifier for the object. READ-ONLY
codestringCoupon code. MANDATORY
date_createddate-timeThe date the coupon was created, in the site's timezone. READ-ONLY
date_modifieddate-timeThe date the coupon was last modified, in the site's timezone. READ-ONLY
descriptionstringCoupon description.
discount_typestringDetermines the type of discount that will be applied. Options: fixed_cart, percent, fixed_product and percent_product. Default: fixed_cart.
amountstringThe amount of discount.
expiry_datestringUTC DateTime when the coupon expires.
usage_countintegerNumber of times the coupon has been used already. READ-ONLY
individual_usebooleanWhether coupon can only be used individually.
product_idsarrayList of product ID's the coupon can be used on.
exclude_product_idsarrayList of product ID's the coupon cannot be used on.
usage_limitintegerHow many times the coupon can be used.
usage_limit_per_userintegerHow many times the coupon can be used per customer.
limit_usage_to_x_itemsintegerMax number of items in the cart the coupon can be applied to.
free_shippingbooleanDefine if can be applied for free shipping.
product_categoriesarrayList of category ID's the coupon applies to.
excluded_product_categoriesarrayList of category ID's the coupon does not apply to.
exclude_sale_itemsbooleanDefine if should not apply when have sale items.
minimum_amountstringMinimum order amount that needs to be in the cart before coupon applies.
maximum_amountstringMaximum order amount allowed when using the coupon.
email_restrictionsarrayList of email addresses that can use this coupon.
used_byarrayList of user IDs who have used the coupon. READ-ONLY

Create a coupon

This API helps you to create a new coupon.

http
POST /wp-json/wc/v1/coupons
<Tabs> <TabItem value="curl" label="cURL">
shell
curl -X POST https://example.com/wp-json/wc/v1/coupons \
	-u consumer_key:consumer_secret \
	-H "Content-Type: application/json" \
	-d '{
  "code": "10off",
  "discount_type": "percent",
  "amount": 10,
  "individual_use": true,
  "exclude_sale_items": true,
  "minimum_amount": "100.00"
}'
</TabItem> <TabItem value="js" label="JavaScript">
javascript
const data = {
  code: "10off",
  discount_type: "percent",
  amount: 10,
  individual_use: true,
  exclude_sale_items: true,
  minimum_amount: "100.00"
};

WooCommerce.post("coupons", data)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });

</TabItem> <TabItem value="php" label="PHP">
php
<?php
$data = [
    'code' => '10off',
    'discount_type' => 'percent',
    'amount' => 10,
    'individual_use' => true,
    'exclude_sale_items' => true,
    'minimum_amount' => '100.00'
];

print_r($woocommerce->post('coupons', $data));
?>
</TabItem> <TabItem value="python" label="Python">
python
data = {
    "code": "10off",
    "discount_type": "percent",
    "amount": 10,
    "individual_use": True,
    "exclude_sale_items": True,
    "minimum_amount": "100.00"
}

print(wcapi.post("coupons", data).json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
data = {
  code: "10off",
  discount_type: "percent",
  amount: 10,
  individual_use: true,
  exclude_sale_items: true,
  minimum_amount: "100.00"
}

woocommerce.post("coupons", data).parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
  "id": 113,
  "code": "10off",
  "date_created": "2016-04-28T21:55:54",
  "date_modified": "2016-04-28T21:55:54",
  "discount_type": "percent",
  "description": "",
  "amount": "10.00",
  "expiry_date": null,
  "usage_count": 0,
  "individual_use": true,
  "product_ids": [],
  "exclude_product_ids": [],
  "usage_limit": null,
  "usage_limit_per_user": null,
  "limit_usage_to_x_items": 0,
  "free_shipping": false,
  "product_categories": [],
  "excluded_product_categories": [],
  "exclude_sale_items": true,
  "minimum_amount": "100.00",
  "maximum_amount": "0.00",
  "email_restrictions": [],
  "used_by": [],
  "_links": {
    "self": [
      {
        "href": "https://example.com/wp-json/wc/v1/coupons/113"
      }
    ],
    "collection": [
      {
        "href": "https://example.com/wp-json/wc/v1/coupons"
      }
    ]
  }
}
</TabItem> </Tabs>

Retrieve a coupon

This API lets you retrieve and view a specific coupon by ID.

http
GET /wp-json/wc/v1/coupons/<id>
<Tabs> <TabItem value="curl" label="cURL">
shell
curl https://example.com/wp-json/wc/v1/coupons/113 \
	-u consumer_key:consumer_secret
</TabItem> <TabItem value="js" label="JavaScript">
javascript
WooCommerce.get("coupons/113")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
</TabItem> <TabItem value="php" label="PHP">
php
<?php print_r($woocommerce->get('coupons/113')); ?>
</TabItem> <TabItem value="python" label="Python">
python
print(wcapi.get("coupons/113").json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
woocommerce.get("coupons/113").parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
  "id": 113,
  "code": "10off",
  "date_created": "2016-04-28T21:55:54",
  "date_modified": "2016-04-28T21:55:54",
  "discount_type": "percent",
  "description": "",
  "amount": "10.00",
  "expiry_date": null,
  "usage_count": 0,
  "individual_use": true,
  "product_ids": [],
  "exclude_product_ids": [],
  "usage_limit": null,
  "usage_limit_per_user": null,
  "limit_usage_to_x_items": 0,
  "free_shipping": false,
  "product_categories": [],
  "excluded_product_categories": [],
  "exclude_sale_items": true,
  "minimum_amount": "100.00",
  "maximum_amount": "0.00",
  "email_restrictions": [],
  "used_by": [],
  "_links": {
    "self": [
      {
        "href": "https://example.com/wp-json/wc/v1/coupons/113"
      }
    ],
    "collection": [
      {
        "href": "https://example.com/wp-json/wc/v1/coupons"
      }
    ]
  }
}
</TabItem> </Tabs>

List all coupons

This API helps you to list all the coupons that have been created.

http
GET /wp-json/wc/v1/coupons
<Tabs> <TabItem value="curl" label="cURL">
shell
curl https://example.com/wp-json/wc/v1/coupons \
	-u consumer_key:consumer_secret
</TabItem> <TabItem value="js" label="JavaScript">
javascript
WooCommerce.get("coupons")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
</TabItem> <TabItem value="php" label="PHP">
php
<?php print_r($woocommerce->get('coupons')); ?>
</TabItem> <TabItem value="python" label="Python">
python
print(wcapi.get("coupons").json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
woocommerce.get("coupons").parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
[
  {
    "id": 114,
    "code": "free-shipping",
    "date_created": "2016-04-28T21:58:25",
    "date_modified": "2016-04-28T21:58:25",
    "discount_type": "fixed_cart",
    "description": "",
    "amount": "0.00",
    "expiry_date": null,
    "usage_count": 0,
    "individual_use": true,
    "product_ids": [],
    "exclude_product_ids": [],
    "usage_limit": null,
    "usage_limit_per_user": null,
    "limit_usage_to_x_items": 0,
    "free_shipping": false,
    "product_categories": [],
    "excluded_product_categories": [],
    "exclude_sale_items": true,
    "minimum_amount": "50.00",
    "maximum_amount": "0.00",
    "email_restrictions": [],
    "used_by": [],
    "_links": {
      "self": [
        {
          "href": "https://example.com/wp-json/wc/v1/coupons/114"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v1/coupons"
        }
      ]
    }
  },
  {
    "id": 113,
    "code": "10off",
    "date_created": "2016-04-28T21:55:54",
    "date_modified": "2016-04-28T21:55:54",
    "discount_type": "percent",
    "description": "",
    "amount": "10.00",
    "expiry_date": null,
    "usage_count": 0,
    "individual_use": true,
    "product_ids": [],
    "exclude_product_ids": [],
    "usage_limit": null,
    "usage_limit_per_user": null,
    "limit_usage_to_x_items": 0,
    "free_shipping": false,
    "product_categories": [],
    "excluded_product_categories": [],
    "exclude_sale_items": true,
    "minimum_amount": "100.00",
    "maximum_amount": "0.00",
    "email_restrictions": [],
    "used_by": [],
    "_links": {
      "self": [
        {
          "href": "https://example.com/wp-json/wc/v1/coupons/113"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v1/coupons"
        }
      ]
    }
  }
]
</TabItem> </Tabs>

Available parameters

ParameterTypeDescription
contextstringScope under which the request is made; determines fields present in response. Options: view and edit.
pageintegerCurrent page of the collection.
per_pageintegerMaximum number of items to be returned in result set.
searchstringLimit results to those matching a string.
afterstringLimit response to resources published after a given ISO8601 compliant date.
beforestringLimit response to resources published before a given ISO8601 compliant date.
dates_are_gmtbooleanInterpret after and before as UTC dates when true.
excludestringEnsure result set excludes specific ids.
includestringLimit result set to specific ids.
offsetintegerOffset the result set by a specific number of items.
orderstringOrder sort attribute ascending or descending. Default is asc. Options: asc and desc.
orderbystringSort collection by object attribute. Default is date, Options: date, id, include, title and slug.
codestringLimit result set to resources with a specific code.

Update a coupon

This API lets you make changes to a coupon.

http
PUT /wp-json/wc/v1/coupons/<id>
<Tabs> <TabItem value="curl" label="cURL">
shell
curl -X PUT https://example.com/wp-json/wc/v1/coupons/113 \
	-u consumer_key:consumer_secret \
	-H "Content-Type: application/json" \
	-d '{
  "amount": 5
}'
</TabItem> <TabItem value="js" label="JavaScript">
javascript
const data = {
  amount: 5
};

WooCommerce.put("coupons/113", data)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
</TabItem> <TabItem value="php" label="PHP">
php
<?php 
$data = [
    'amount' => 5
];

print_r($woocommerce->put('coupons/113', $data)); 
?>
</TabItem> <TabItem value="python" label="Python">
python
data = {
    "amount": 5
}

print(wcapi.put("coupons/113", data).json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
data = {
  amount: 5
}

woocommerce.put("coupons/113", data).parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
  "id": 113,
  "code": "10off",
  "date_created": "2016-04-28T21:55:54",
  "date_modified": "2016-04-28T22:00:49",
  "discount_type": "percent",
  "description": "",
  "amount": "5.00",
  "expiry_date": null,
  "usage_count": 0,
  "individual_use": true,
  "product_ids": [],
  "exclude_product_ids": [],
  "usage_limit": null,
  "usage_limit_per_user": null,
  "limit_usage_to_x_items": 0,
  "free_shipping": false,
  "product_categories": [],
  "excluded_product_categories": [],
  "exclude_sale_items": true,
  "minimum_amount": "100.00",
  "maximum_amount": "0.00",
  "email_restrictions": [],
  "used_by": [],
  "_links": {
    "self": [
      {
        "href": "https://example.com/wp-json/wc/v1/coupons/113"
      }
    ],
    "collection": [
      {
        "href": "https://example.com/wp-json/wc/v1/coupons"
      }
    ]
  }
}
</TabItem> </Tabs>

Delete a coupon

This API helps you delete a coupon.

http
DELETE /wp-json/wc/v1/coupons/<id>
<Tabs> <TabItem value="curl" label="cURL">
shell
curl -X DELETE https://example.com/wp-json/wc/v1/coupons/113?force=true \
	-u consumer_key:consumer_secret
</TabItem> <TabItem value="js" label="JavaScript">
javascript
WooCommerce.delete("coupons/113", {
  force: true
})
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
</TabItem> <TabItem value="php" label="PHP">
php
<?php print_r($woocommerce->delete('coupons/113', ['force' => true])); ?>
</TabItem> <TabItem value="python" label="Python">
python
print(wcapi.delete("coupons/113", params={"force": True}).json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
woocommerce.delete("coupons/113", force: true).parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
  "id": 113,
  "code": "10off",
  "date_created": "2016-04-28T21:55:54",
  "date_modified": "2016-04-28T22:00:49",
  "discount_type": "percent",
  "description": "",
  "amount": "5.00",
  "expiry_date": null,
  "usage_count": 0,
  "individual_use": true,
  "product_ids": [],
  "exclude_product_ids": [],
  "usage_limit": null,
  "usage_limit_per_user": null,
  "limit_usage_to_x_items": 0,
  "free_shipping": false,
  "product_categories": [],
  "excluded_product_categories": [],
  "exclude_sale_items": true,
  "minimum_amount": "100.00",
  "maximum_amount": "0.00",
  "email_restrictions": [],
  "used_by": [],
  "_links": {
    "self": [
      {
        "href": "https://example.com/wp-json/wc/v1/coupons/113"
      }
    ],
    "collection": [
      {
        "href": "https://example.com/wp-json/wc/v1/coupons"
      }
    ]
  }
}
</TabItem> </Tabs>

Available parameters

ParameterTypeDescription
forcestringUse true whether to permanently delete the coupon, Default is false.

Batch update coupons

This API helps you to batch create, update and delete multiple coupons.

:::note Note: By default it's limited to up to 100 objects to be created, updated or deleted. :::

http
POST /wp-json/wc/v1/coupons/batch
<Tabs> <TabItem value="curl" label="cURL">
shell
curl -X POST https://example.com/wp-json/wc/v1/coupons/batch \
	-u consumer_key:consumer_secret \
	-H "Content-Type: application/json" \
	-d '{
  "create": [
    {
      "code": "20off",
      "discount_type": "percent",
      "amount": 20,
      "individual_use": true,
      "exclude_sale_items": true,
      "minimum_amount": "100.00"
    },
    {
      "code": "30off",
      "discount_type": "percent",
      "amount": 30,
      "individual_use": true,
      "exclude_sale_items": true,
      "minimum_amount": "100.00"
    }
  ],
  "update": [
    {
      "id": 113,
      "minimum_amount": "50.00"
    }
  ],
  "delete": [
    137
  ]
}'
</TabItem> <TabItem value="js" label="JavaScript">
javascript
const data = {
  create: [
    {
      code: "20off",
      discount_type: "percent",
      amount: 20,
      individual_use: true,
      exclude_sale_items: true,
      minimum_amount: "100.00"
    },
    {
      code: "30off",
      discount_type: "percent",
      amount: 30,
      individual_use: true,
      exclude_sale_items: true,
      minimum_amount: "100.00"
    }
  ],
  update: [
    {
      id: 113,
      minimum_amount: "50.00"
    }
  ],
  delete: [
    137
  ]
};

WooCommerce.post("coupons/batch", data)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
</TabItem> <TabItem value="php" label="PHP">
php
<?php
$data = [
    'create' => [
        [
            'code' => '20off',
            'discount_type' => 'percent',
            'amount' => 20,
            'individual_use' => true,
            'exclude_sale_items' => true,
            'minimum_amount' => '100.00'
        ],
        [
            'code' => '30off',
            'discount_type' => 'percent',
            'amount' => 30,
            'individual_use' => true,
            'exclude_sale_items' => true,
            'minimum_amount' => '100.00'
        ]
    ],
    'update' => [
        [
            'id' => 113,
            'minimum_amount' => '50.00'
        ]
    ],
    'delete' => [
        137
    ]
];

print_r($woocommerce->post('coupons/batch', $data));
?>
</TabItem> <TabItem value="python" label="Python">
python
data = {
    "create": [
        {
            "code": "20off",
            "discount_type": "percent",
            "amount": 20,
            "individual_use": True,
            "exclude_sale_items": True,
            "minimum_amount": "100.00"
        },
        {
            "code": "30off",
            "discount_type": "percent",
            "amount": 30,
            "individual_use": True,
            "exclude_sale_items": True,
            "minimum_amount": "100.00"
        }
    ],
    "update": [
        {
            "id": 113,
            "minimum_amount": "50.00"
        }
    ],
    "delete": [
        137
    ]
}

print(wcapi.post("coupons/batch", data).json())
</TabItem> <TabItem value="ruby" label="Ruby">
ruby
data = {
  create: [
    {
      code: "20off",
      discount_type: "percent",
      amount: 20,
      individual_use: true,
      exclude_sale_items: true,
      minimum_amount: "100.00"
    },
    {
      code: "30off",
      discount_type: "percent",
      amount: 30,
      individual_use: true,
      exclude_sale_items: true,
      minimum_amount: "100.00"
    }
  ],
  update: [
    {
      id: 113,
      minimum_amount: "50.00"
    }
  ],
  delete: [
    137
  ]
}

woocommerce.post("coupons/batch", data).parsed_response
</TabItem> <TabItem value="response" label="JSON Response">
json
{
  "create": [
    {
      "id": 138,
      "code": "20off",
      "date_created": "2016-05-17T20:52:21",
      "date_modified": "2016-05-17T20:52:21",
      "discount_type": "percent",
      "description": "",
      "amount": "20.00",
      "expiry_date": null,
      "usage_count": 0,
      "individual_use": true,
      "product_ids": [],
      "exclude_product_ids": [],
      "usage_limit": null,
      "usage_limit_per_user": null,
      "limit_usage_to_x_items": 0,
      "free_shipping": false,
      "product_categories": [],
      "excluded_product_categories": [],
      "exclude_sale_items": true,
      "minimum_amount": "100.00",
      "maximum_amount": "0.00",
      "email_restrictions": [],
      "used_by": [],
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v1/coupons/138"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v1/coupons"
          }
        ]
      }
    },
    {
      "id": 139,
      "code": "30off",
      "date_created": "2016-05-17T20:52:22",
      "date_modified": "2016-05-17T20:52:22",
      "discount_type": "percent",
      "description": "",
      "amount": "30.00",
      "expiry_date": null,
      "usage_count": 0,
      "individual_use": true,
      "product_ids": [],
      "exclude_product_ids": [],
      "usage_limit": null,
      "usage_limit_per_user": null,
      "limit_usage_to_x_items": 0,
      "free_shipping": false,
      "product_categories": [],
      "excluded_product_categories": [],
      "exclude_sale_items": true,
      "minimum_amount": "100.00",
      "maximum_amount": "0.00",
      "email_restrictions": [],
      "used_by": [],
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v1/coupons/139"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v1/coupons"
          }
        ]
      }
    }
  ],
  "update": [
    {
      "id": 113,
      "code": "10off",
      "date_created": "2016-04-28T21:55:54",
      "date_modified": "2016-05-17T20:52:23",
      "discount_type": "percent",
      "description": "",
      "amount": "5.00",
      "expiry_date": null,
      "usage_count": 0,
      "individual_use": true,
      "product_ids": [],
      "exclude_product_ids": [],
      "usage_limit": null,
      "usage_limit_per_user": null,
      "limit_usage_to_x_items": 0,
      "free_shipping": false,
      "product_categories": [],
      "excluded_product_categories": [],
      "exclude_sale_items": true,
      "minimum_amount": "50.00",
      "maximum_amount": "0.00",
      "email_restrictions": [],
      "used_by": [],
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v1/coupons/113"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v1/coupons"
          }
        ]
      }
    }
  ],
  "delete": [
    {
      "id": 137,
      "code": "50off",
      "date_created": "2016-05-17T20:49:12",
      "date_modified": "2016-05-17T20:50:30",
      "discount_type": "fixed_cart",
      "description": "",
      "amount": "50.00",
      "expiry_date": null,
      "usage_count": 0,
      "individual_use": false,
      "product_ids": [],
      "exclude_product_ids": [],
      "usage_limit": null,
      "usage_limit_per_user": null,
      "limit_usage_to_x_items": 0,
      "free_shipping": false,
      "product_categories": [],
      "excluded_product_categories": [],
      "exclude_sale_items": false,
      "minimum_amount": "0.00",
      "maximum_amount": "0.00",
      "email_restrictions": [],
      "used_by": [],
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v1/coupons/137"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v1/coupons"
          }
        ]
      }
    }
  ]
}
</TabItem> </Tabs>