# Promotions

Source: https://developers.swell.is/backend-api/promotions

Promotions are a way to offer customers a discount without a coupon code, by automatically applying discounts to their cart. A promotion can have several discounts and exceptions. For example, a promotion discount can apply to a whole order, to particular categories, to individual products, or to shipping methods. A promotion can be limited to a maximum number of uses, by specific customer groups, and more.

## The promotion model

### Fields

- `id` (objectId): Unique identifier for the promotion.
- `name` (string, required): A short descriptive name of the promo.
- `active` (boolean): Indicates the promo is currently active. Note: this is not affected by `date_start` and `date_end`. Default: `false`.
- `currency` (string): Three-letter ISO currency code in uppercase. Defaults to the store's base currency.
- `date_created` (date, auto): Date and time the promotion was created.
- `date_end` (date): Date the promo ends and is no longer available. When defined, the promo will not be applied after this date.
- `date_start` (date): Date the promo is first available. When defined, the promo will not be valid until after this date and before `date_end`.
- `date_updated` (date, auto): Date and time the promotion was last updated.
- `description` (string): A brief description of the promo, as it may be displayed to customers.
- `discount_group` (string): ID of the discount group the promo belongs to, if applicable, as defined in discount settings. Used to ensure only one set of discount rules can apply across a number of coupons and promotions.
- `discounts` (array of object): List of discount rules to apply.
  - `buy_items` (array of object): Items required to be purchased in order to receive the promotion.
    - `category_id` (objectId): Unique identifier for the category.
    - `category` (category): Expandable link to the category.
    - `product_id` (objectId): Unique identifier for the product.
    - `product` (product): Expandable link to the product.
  - `category` (category): Expandable link to the discounted category, if applicable.
  - `category_id` (objectId): ID of the category to discount, if applicable.
  - `exclude_categories` (exclude_categories): Expandable list of excluded categories, if applicable.
  - `exclude_category_ids` (array of category_id): List of category IDs to exclude from discount, applicable to category discounts when several categories overlap.
  - `get_items` (array of object): Eligible discounted items available once promotion's `buy_items` requirements are met.
    - `category_id` (objectId): Unique identifier for the category.
    - `category` (category): Expandable link to the category.
    - `product_id` (objectId): Unique identifier for the product.
    - `product` (product): Expandable link to the product.
  - `get_total` (boolean): Indicates whether the `get_items` promotion requirements have been met.
  - `price_min` (currency): Minimum product price required for the rule to apply, applicable for category and product discounts.
  - `product` (product): Expandable link to the discounted product, if applicable.
  - `product_id` (objectId): ID of the product to discount, if applicable.
  - `quantity_max` (int): Maximum product quantity for which the rule will apply, applicable for category and product discounts.
  - `quantity_min` (int): Minimum product quantity required for the rule to apply, applicable for category and product discounts.
  - `shipment_service` (string): ID of the shipping service to discount, if applicable.
  - `total_min` (currency): Minimum order total required for the rule to apply.
  - `type` (enum): Type of discount to apply. Can be `total`, `product`, `category` or `shipment`. Possible values: `total`, `shipment`, `product`, `category`, `buy_get`. Default: `"total"`.
  - `value_fixed` (currency): Fixed discount applied when `value_type=fixed`.
  - `value_percent` (float): Percentage discount applied when `value_type=percent`.
  - `variant_id` (objectId): ID of the variant to discount, if applicable.
  - `value_type` (enum, required): Type of discount amount to calculate. Can be `fixed` or `percent`. Possible values: `fixed`, `percent`. Default: `"fixed"`.
  - `purchase_option` (enum): Purchase option the discount applies to. Possible values: `standard`, `subscription`, `subscription_plan`.
  - `subscription_plan_id` (objectId): ID of the subscription plan the discount applies to, when `purchase_option` is `subscription_plan`.
  - `variant` (variant): Expandable link to the discounted variant, if applicable.
- `exclusions` (array of object): List of exclusions considered when the promo is applied.
  - `category_id` (objectId): ID of the category to exclude from the promo.
  - `category` (category): Expandable link to the category excluded from the promo.
  - `product_id` (objectId): ID of the product to exclude from the promo.
  - `product` (product): Expandable link to the product excluded from the promo.
  - `type` (enum, required): Type of exclusion. Can be `product` or `category`. Possible values: `product`, `category`.
- `limit_account_groups` (array of account_groups): List of customer account for which the promo will apply.
- `limit_account_uses` (int): Maximum number of times the promo will be applied by each customer account.
- `limit_uses` (int): Maximum number of times the promo will be applied across all customers.
- `orders` (Order): Expandable list of orders that have applied the promo.
- `subscriptions` (Subscription): Expandable list of subscriptions that have applied the promo.
- `use_count` (int, auto): Number of times the promo has been applied. Default: `0`.
- `uses` (array of Uses): Expandable list of promotion use records.

### Example response

```json
{
  "id": "60f199509111e7000000006b",
  "name": "15% Off Store Wide Sale",
  "active": true,
  "currency": "USD",
  "date_created": "2021-07-16T14:36:00.386Z",
  "date_end": "2018-03-01T00:00:00.000Z",
  "date_start": "2018-11-01T00:00:00.000Z",
  "date_updated": "2021-07-16T14:36:00.386Z",
  "description": "Save 15% store wide for a limited time only.",
  "discounts": [
    {
      "type": "total",
      "value_type": "percent",
      "value_fixed": 15
    }
  ],
  "limit_account_uses": 3,
  "limit_uses": 300,
  "use_count": 142
}
```


## Create a promotion

Create a new promotion.

### Arguments

- `name` (string, required): A short descriptive name of the promo.
- `description` (string): A brief description of the promo, as it may be displayed to customers.
- `active` (boolean): Indicates the promo is currently active. Note: this is not affected by `date_start` and `date_end`. Default: `false`.
- `date_start` (date): Date the promo is first available. When defined, the promo will not be valid until after this date and before `date_end`.
- `date_end` (date): Date the promo ends and is no longer available. When defined, the promo will not be applied after this date.
- `discounts` (array of object, required): List of discount rules to apply.
  - `type` (enum): Type of discount to apply. Can be `total`, `product`, `category`, or `shipment`. Possible values: `total`, `shipment`, `product`, `category`, `buy_get`. Default: `"total"`.
  - `value_type` (enum, required): Type of discount amount to calculate. Can be `fixed` or `percent`. Possible values: `fixed`, `percent`. Default: `"fixed"`.
  - `value_fixed` (currency): Fixed discount applied when `value_type=fixed`.
  - `value_percent` (float): Percentage discount applied when `value_type=percent`.
  - `total_min` (currency): Minimum order total required for the rule to apply.
  - `discount_max` (currency): The maximum amount of a discount that can be redeemed.
  - `price_min` (currency): The minimum amount of a discount that can be redeemed.
  - `quantity_min` (int): Minimum product quantity required for the rule to apply, applicable for category and product discounts.
  - `quantity_max` (int): Maximum product quantity for which the rule will apply, applicable for category and product discounts.
  - `product_id` (objectId): ID of the product to discount, if applicable.
  - `variant_id` (objectId): ID of the variant to discount, if applicable.
  - `shipment_service` (string): ID of the shipping service to discount, if applicable.
  - `category_id` (objectId): ID of the category to discount, if applicable.
  - `exclude_category_ids` (array of child_scalar): List of category IDs to exclude from discount, applicable to category discounts when several categories overlap.
  - `product` (product): Expandable link to the discounted product, if applicable.
  - `variant` (variant): Expandable link to the discounted variant, if applicable.
  - `category` (category): Expandable link to the discounted category, if applicable.
  - `exclude_categories` (exclude_categories): Expandable list of excluded categories, if applicable.
  - `buy_items` (array of object): Items required to be purchased in order to receive the promotion.
    - `product_id` (objectId): ID of the product that is tied to the promotion eligibility, if applicable.
    - `product` (product): Expandable link to the promotion-eligible product, if applicable.
    - `category_id` (objectId): ID of the category that is tied to the promotion eligibility, if applicable.
    - `category` (category): Expandable link to the promotion-eligible category, if applicable.
  - `get_items` (array of object): Eligible discounted items available once  promotion's `buy_items` requirements are met.
    - `product_id` (objectId): ID of the product to discount, if applicable.
    - `product` (product): Expandable link to the discounted product, if applicable.
    - `category_id` (objectId): ID of the discounted category, if applicable.
    - `category` (category): Expandable link to the discounted category, if applicable.
- `use_count` (int): Number of times the promo has been applied. Default: `0`.
- `limit_uses` (int): Maximum number of times the promo will be applied across all customers.
- `limit_account_uses` (int): Maximum number of times the promo will be applied by each customer account.
- `limit_account_groups` (array of child_scalar): List of customer account for which the promo will apply.
- `discount_group` (string): ID of the discount group the promo belongs to, if applicable, as defined in discount settings. Used to ensure only one set of discount rules can apply across a number of coupons and promotions.
- `exclusions` (array of object): List of exclusions considered when the promo is applied.
  - `type` (enum, required): The type of exclusion for the promotion. Can be either `product` or `category`. Possible values: `product`, `category`.
  - `product_id` (objectId): ID of the product to exclude from the discount, if applicable.
  - `category_id` (objectId): ID of the category to exclude from the discount, if applicable.
  - `product` (product): Expandable link to the excluded product, if applicable.
  - `category` (category): Expandable link to the excluded category, if applicable.
- `uses` (Promotion Use): Expandable list of promo usage records.
- `orders` (Order): Expandable list of orders that have applied the promo.
- `subscriptions` (Subscription): Expandable list of subscriptions that have applied the promo.
- `currency` (string): Three-letter ISO currency code in uppercase. Defaults to the store's base currency.
- `limit_account_segments` (array of child_scalar)

### Example request

`POST /promotions`

**cURL**

```bash
$ curl https://api.swell.store/promotions \
  -u store-id:secret-key \
  -d discounts[0][type]=total \
  -d discounts[0][value_type]=percent \
  -d discounts[0][value_fixed]=15 \
  -d name="15% Off Store Wide Sale" \
  -d active=true \
  -d date_end=2018-03-01T00:00:00.000Z \
  -d date_start=2018-11-01T00:00:00.000Z \
  -d description="Save 15% store wide for a limited time only." \
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.post('/promotions', {
  discounts: [
    {
      type: 'total',
      value_type: 'percent',
      value_fixed: 15
    }
  ],
  name: '15% Off Store Wide Sale',
  active: true,
  date_end: '2018-03-01T00:00:00.000Z',
  date_start: '2018-11-01T00:00:00.000Z',
  description: 'Save 15% store wide for a limited time only.'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->post('/promotions', [
  'discounts' => [
    [
      'type' => 'total',
      'value_type' => 'percent',
      'value_fixed' => 15
    ]
  ],
  'name' => '15% Off Store Wide Sale',
  'active' => true,
  'date_end' => '2018-03-01T00:00:00.000Z',
  'date_start' => '2018-11-01T00:00:00.000Z',
  'description' => 'Save 15% store wide for a limited time only.'
]);
```

### Example response

```json
{
  "id": "60f199509111e7000000006b",
  "name": "15% Off Store Wide Sale",
  "active": true,
  "currency": "USD",
  "date_created": "2021-07-16T14:36:00.386Z",
  "date_end": "2018-03-01T00:00:00.000Z",
  "date_start": "2018-11-01T00:00:00.000Z",
  "date_updated": "2021-07-16T14:36:00.386Z",
  "description": "Save 15% store wide for a limited time only.",
  "discounts": [
    {
      "type": "total",
      "value_type": "percent",
      "value_fixed": 15
    }
  ],
  "limit_account_uses": 3,
  "limit_uses": 300,
  "use_count": 142
}
```


## Retrieve a promotion

Retrieve an existing promotion using the ID that was returned when created.

### Arguments

- `id` (objectId, required): The ID of the promotion to retrieve.
- `expand` (string): Expanding link fields and child collections is performed using the expand argument.

  - For example, `expand=account` would return a related customer account if one exists.

  When the field represents a collection, you can specify the query limit.

  - For example, `expand=variants:10` would return up to 10 records of the variants collection.

  See [expanding ](https://developers.swell.is/backend-api/querying/expanding)for more details.
- `fields` (string): Return only the specified fields in the result. For example `fields=name,slug` would return only the fields `name` and `slug` in the response. Supports nested object and array fields using dot-notation, for example, `items.product_id`. The category `id` is always returned.
- `include` (string): Include one or more arbitrary queries in the response, possibly related to the main query.

  See [including ](https://developers.swell.is/backend-api/querying/including)for more details.

### Example request

`GET /promotions/:id`

**cURL**

```bash
$ curl https://api.swell.store/promotions/60f199509111e70000000071 \
  -u store-id:secret-key
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.get('/promotions/{id}', {
  id: '60f199509111e70000000071'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->get('/promotions/{id}', [
  'id' => '60f199509111e70000000071'
]);
```

### Example response

```json
{
  "id": "60f199509111e70000000071",
  "name": "15% Off Store Wide Sale",
  "active": true,
  "currency": "USD",
  "date_created": "2021-07-16T14:36:00.386Z",
  "date_end": "2018-03-01T00:00:00.000Z",
  "date_start": "2018-11-01T00:00:00.000Z",
  "date_updated": "2021-07-16T14:36:00.386Z",
  "description": "Save 15% store wide for a limited time only.",
  "discounts": [
    {
      "type": "total",
      "value_type": "percent",
      "value_fixed": 15
    }
  ],
  "limit_account_uses": 3,
  "limit_uses": 300,
  "use_count": 142
}
```


## Update a promotion

Update an existing promotion using the ID that was returned when created.

### Arguments

- `id` (objectId, required): Unique identifier for the promotion.
- `active` (boolean): Indicates the promo is currently active. Note: this is not affected by `date_start` and `date_end`. Default: `false`.
- `date_end` (date): Date the promo ends and is no longer available. When defined, the promo will not be applied after this date.
- `name` (string): A short descriptive name of the promo.
- `currency` (string): Three-letter ISO currency code in uppercase. Defaults to the store's base currency.
- `date_start` (date): Date the promo is first available. When defined, the promo will not be valid until after this date and before `date_end`.
- `description` (string): A brief description of the promo, as it may be displayed to customers.
- `discount_group` (string): ID of the discount group the promo belongs to, if applicable, as defined in discount settings. Used to ensure only one set of discount rules can apply across a number of coupons and promotions.
- `discounts` (array of object, required): List of discount rules to apply.
  - `type` (enum): Type of discount to apply. Can be `total`, `product`, `category`, or `shipment`. Possible values: `total`, `shipment`, `product`, `category`, `buy_get`. Default: `"total"`.
  - `value_type` (enum, required): Type of discount amount to calculate. Can be `fixed` or `percent`. Possible values: `fixed`, `percent`. Default: `"fixed"`.
  - `value_fixed` (currency): Fixed discount applied when `value_type=fixed`.
  - `value_percent` (float): Percentage discount applied when `value_type=percent`.
  - `total_min` (currency): Minimum order total required for the rule to apply.
  - `discount_max` (currency): The maximum amount of a discount that can be redeemed.
  - `price_min` (currency): The mimimum amount of a discount that can be redeemed.
  - `quantity_min` (int): Minimum product quantity required for the rule to apply, applicable for category and product discounts.
  - `quantity_max` (int): Maximum product quantity for which the rule will apply, applicable for category and product discounts.
  - `product_id` (objectId): ID of the product to discount, if applicable.
  - `variant_id` (objectId): ID of the variant to discount, if applicable.
  - `shipment_service` (string): ID of the shipping service to discount, if applicable.
  - `category_id` (objectId): ID of the category to discount, if applicable.
  - `exclude_category_ids` (array of child_scalar): List of category IDs to exclude from discount, applicable to category discounts when several categories overlap.
  - `product` (product): Expandable link to the discounted product, if applicable.
  - `variant` (variant): Expandable link to the discounted variant, if applicable.
  - `category` (category): Expandable link to the discounted category, if applicable.
  - `exclude_categories` (exclude_categories): Expandable list of excluded categories, if applicable.
  - `buy_items` (array of object): List of items that need to be purchased before the promotion becomes eligible.
    - `product_id` (objectId): ID of the product that is tied to the promotion eligibility, if applicable.
    - `product` (product): Expandable link to the promotion-eligible product, if applicable.
    - `category_id` (objectId): ID of the category that is tied to the promotion eligibility, if applicable.
    - `category` (category): Expandable link to the promotion-eligible category, if applicable.
  - `get_items` (array of object): List of items eligible for promotion after designated products have been purchased.
    - `product_id` (objectId): ID of the product to discount, if applicable.
    - `product` (product): Expandable link to the discounted product, if applicable.
    - `category_id` (objectId): ID of the discounted category, if applicable.
    - `category` (category): Expandable link to the discounted category, if applicable.
- `exclusions` (array of object): List of exclusions considered when the promo is applied.
  - `type` (enum, required): The type of exclusion for the promotion. Can be either `product` or `category`. Possible values: `product`, `category`.
  - `product_id` (objectId): ID of the product to exclude from the discount, if applicable.
  - `category_id` (objectId): ID of the category to exclude from the discount, if applicable.
  - `product` (product): Expandable link to the excluded product, if applicable.
  - `category` (category): Expandable link to the excluded category, if applicable.
- `use_count` (int): Number of times the promo has been applied. Default: `0`.
- `limit_uses` (int): Maximum number of times the promo will be applied across all customers.
- `limit_account_uses` (int): Maximum number of times the promo will be applied by each customer account.
- `limit_account_groups` (array of child_scalar): List of customer account for which the promo will apply.

### Example request

`PUT /promotions/:id`

**cURL**

```sh
$ curl https://api.swell.store/promotions/60f199509111e7000000006b \
  -u store-id:secret-key \
  -d limit_uses=300 \
  -d active=true \
  -X PUT
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.put('/promotions/{id}', {
  id: '60f199509111e7000000006b',
  limit_uses: 300,
  active: true
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->put('/promotions/{id}', [
  'id' => '60f199509111e7000000006b',
  'limit_uses' => 300,
  'active' => true
]);
```

### Example response

```json
{
  "id": "60f199509111e7000000006b",
  "name": "15% Off Store Wide Sale",
  "active": true,
  "currency": "USD",
  "date_created": "2021-07-16T14:36:00.386Z",
  "date_end": "2018-03-01T00:00:00.000Z",
  "date_start": "2018-11-01T00:00:00.000Z",
  "date_updated": "2021-07-16T14:36:00.386Z",
  "description": "Save 15% store wide for a limited time only.",
  "discounts": [
    {
      "type": "total",
      "value_type": "percent",
      "value_fixed": 15
    }
  ],
  "limit_account_uses": 3,
  "limit_uses": 300,
  "use_count": 142
}
```


## List all promotions

Return a list of promotions.

### Arguments

- `expand` (string): Expand link fields and child collections by using the expand argument.

  - For example, `expand=account` would return a related customer account if one exists.

  When the field represents a collection, you can specify the query limit.

  - For example, `expand=variants:10` would return up to 10 records of the variants collection.

  See [expanding](https://developers.swell.is/backend-api/querying/expanding) for more details.
- `fields` (string): Returns only the specified fields in the result.

  - For example `fields=name,slug` would return only the fields `name` and `slug` in the response.

  Supports nested object and array fields using dot-notation.

  - For example, `items.product_id`. The product `id` is always returned.
- `include` (object): Include one or more arbitrary queries in the response which are potentially related to the main query.

  See [including](https://developers.swell.is/backend-api/querying/including) for more details.
- `limit` (int): Limit the number of records returned, ranging between `1` and `1000`. Defaults to `15`. Default: `15`.
- `page` (int): The page number of results to return given the specified or default `limit`.
- `search` (string): A text search is performed using the search argument. Searchable fields are defined by the model.

  - For example, `search=red` would return records containing the word "red" anywhere in the defined text fields.

  See [searching](https://developers.swell.is/backend-api/querying/searching) for more details.
- `sort` (string): Expression to sort results by using a format similar to a SQL sort statement.

  - For example, `sort=name asc` would return records sorted by name ascending.

  See [sorting](https://developers.swell.is/backend-api/querying/sorting) for more details.
- `where` (object): An object with criteria to filter the result.

  - For example, `active=true` would return records containing a field `active` with the value `true`.

  It's also possible to use query operators, for example, `$eq`, `$ne`, `$gt`, and more.

  See [querying](https://developers.swell.is/backend-api/querying) for more details.

### Example request

`GET /promotions`

**cURL**

```bash
$ curl https://api.swell.store/promotions?limit=25&page=1 \
  -u store-id:secret-key \
  -G
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.get('/promotions', {
  where: {
    active: true
  },
  limit: 25,
  page: 1
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->get('/promotions', [
  'where' => [
    'active' => true
  ],
  'limit' => 25,
  'page' => 1
]);
```

### Example response

```json
{
  "count": 51,
  "results": [
    {
      "id": "60f199509111e7000000006b",
      "name": "15% Off Store Wide Sale",
      "active": true,
      "currency": "USD",
      "date_created": "2021-07-16T14:36:00.386Z",
      "date_end": "2018-03-01T00:00:00.000Z",
      "date_start": "2018-11-01T00:00:00.000Z",
      "date_updated": "2021-07-16T14:36:00.386Z",
      "description": "Save 15% store wide for a limited time only.",
      "discounts": [
        {
          "type": "total",
          "value_type": "percent",
          "value_fixed": 15
        }
      ],
      "limit_account_uses": 3,
      "limit_uses": 300,
      "use_count": 142
    },
    {...},
    {...}
  ],
  "page": 1,
  "page_count": 3,
  "limit": 25,
  "pages": {
    "1": {
      "start": 1,
      "end": 25
    },
    "2": {
      "start": 26,
      "end": 50
    },
    "3": {
      "start": 51,
      "end": 51
    }
  }
}
```


## Delete a promotion

Delete a promotion permanently.

### Arguments

- `id` (objectId, required): The id of the promotion to delete.

### Example request

`DELETE /promotions/:id`

**cURL**

```bash
$ curl https://api.swell.store/promotions/60f199509111e70000000077 \
  -u store-id:secret-key \
  -X DELETE
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.delete('/promotions/{id}', {
  id: '60f199509111e70000000077'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->delete('/promotions/{id}', [
  'id' => '60f199509111e70000000077'
]);
```

### Example response

```json
{
  "id": "60f199509111e70000000077",
  "name": "15% Off Store Wide Sale",
  "active": true,
  "currency": "USD",
  "date_created": "2021-07-16T14:36:00.386Z",
  "date_end": "2018-03-01T00:00:00.000Z",
  "date_start": "2018-11-01T00:00:00.000Z",
  "date_updated": "2021-07-16T14:36:00.386Z",
  "description": "Save 15% store wide for a limited time only.",
  "discounts": [
    {
      "type": "total",
      "value_type": "percent",
      "value_fixed": 15
    }
  ],
  "limit_account_uses": 3,
  "limit_uses": 300,
  "use_count": 142
}
```

