# Update a product

Source: https://developers.swell.is/backend-api/products/update-a-product

Updates an existing product using the ID that was returned when created. Updating performs a merge operation. To explicitly override values such as arrays, use the `$set` operator.

## Arguments

- `id` (objectId, required): Unique identifier for the product.
- `purchase_options` (object): Configuration of one or more purchase options for the product. Can be `standard` for one-time purchases or `subscription` for a subscription plan. Products can support both purchase options simultaneously.
  - `standard` (object): Designates purchase option as a one-time purchase.
    - `id` (objectId): ID of the purchase option.
    - `name` (string, required): The name of the purchase option.
    - `description` (string): Indicates whether the purchase option is available for customers to purchase. Inactive products will not be returned on the [Frontend](https://developers.swell.is/frontend-api/introduction).
    - `active` (boolean): Whether the purchase option is available for customers to purchase. Inactive products will not be returned on the [Frontend](https://developers.swell.is/frontend-api/introduction). Default: `false`.
    - `price` (currency): List price used when `sale=false` or `sale_price` is not defined.
    - `sale` (boolean): Indicates whether the product option is on sale. If `true`, the `sale_price` will be used by default when the product is added to a cart.
    - `sale_price` (currency): Sale price used by default when `sale=true`, overriding `price`. Overrides product sale price.
    - `prices` (array of object): Price rules determined by cart quantity or customer account group. Overrides `price` and `sale_price` when conditions match.
      - `price` (currency, required): Price applied when conditions are met.
      - `account_group` (string): Customer account group as a condition to apply price.
      - `quantity_max` (int): Maximum quantity as a condition to apply price.
      - `quantity_min` (int): Minimum quantity as a condition to apply price.
    - `account_groups` (array of string): Array of account groups that are eligible to access the purchase option within the storefront.
  - `subscription` (object): Designates purchase option as a subscription plan.
    - `id` (objectId, auto): ID of the subscription plan purchase option.
    - `name` (string, required): Name of the subscription plan purchase option.
    - `description` (string): A long-form description of the purchase option. May contain HTML or other markup languages.
    - `active` (boolean): Indicates whether the purchase option is available for customers to purchase. Inactive products will not be returned on the [Frontend](https://developers.swell.is/frontend-api/introduction).
    - `account_groups` (array of string): Array of `account_group` names for which the purchase option is available.
    - `plans` (array of plans): Array defining subscription plans and their respective configurations.
      - `id` (objectId): ID of the purchase option subscription plan.
      - `name` (string): Name of the subscription plan.
      - `description` (string): A long-form description of the subscription plan. May contain HTML or other markup languages.
      - `active` (boolean): Indicates whether the subscription plan is available for customers to purchase. Inactive products will not be returned on the [Frontend](https://developers.swell.is/frontend-api/introduction). Default: `false`.
      - `price` (currency): List price used when `sale=false` or `sale_price` is not defined.
      - `prices` (array of object): Price rules determined by cart quantity or customer account group. Overrides `price` and `sale_price` when conditions match.
        - `price` (currency): Price applied when conditions are met.
        - `account_group` (string): Customer account group as a condition to apply price.
        - `quantity_max` (int): Maximum quantity as a condition to apply price.
        - `quantity_min` (int): Minimum quantity as a condition to apply price.
      - `billing_schedule` (object, required): Determines the billing schedule for the subscription plan.
        - `interval` (enum): Subscription plan billing interval. Can be `daily`, `weekly`, `monthly`, or `yearly`. Possible values: `daily`, `weekly`, `monthly`, `yearly`. Default: `monthly`.
        - `interval_count` (int, required): Multiplier for billing interval. For example, to make the billing cycle once every two weeks, set `interval=weekly` and `interval_count=2`. Default: `1`.
        - `limit` (int): Specifies a limit to the number of billing cycles for the subscription plan. For example, `"limit"=10` would stop billing the customer after the tenth billing cycle. Default: `∞`.
- `active` (boolean): Set `true` to make the product visible to customers in a storefront, otherwise it will be hidden.
- `attributes` (object): An object containing custom attribute key/value pairs. See [attributes](https://developers.swell.is/backend-api/attributes/the-attribute-model) for more details.
- `name` (string): Human-friendly name of the product.
- `price` (currency): List price used when `sale=false` or `sale_price` is not defined. This value is intended for use via the frontend. See the `purchase_options` array to manage a product's price.
- `slug` (string): Lowercase, hyphenated identifier typically used in URLs. When creating a product, a `slug` will be generated automatically from the `name`. Maximum length of 1,000 characters.
- `bundle` (boolean): Indicates whether the product is a bundle of other products.
- `bundle_items` (object): List of products sold as a bundle. Applicable only when `bundle=true`.
  - `product_id` (objectId): The id of the bundled product.
  - `quantity` (int): Quantity of the bundled product. Default: `1`.
  - `variant_id` (objectId): The id of the bundled variant, if applicable.
- `category_id` (objectId): Primary category, commonly used as a navigation anchor.
- `cost` (currency): Cost of goods (COGS) used to calculate gross margins.
- `cross_sells` (object): List of products to display as cross-sells on a shopping cart page.
  - `product_id` (objectId): The id of the cross-sell product.
  - `discount_amount` (currency): Discount to apply as a fixed amount. Applicable only when `discount_type=fixed`
  - `discount_percent` (float): Discount to apply as a percentage. Applicable only when `discount_type=percent`.
  - `discount_type` (string): Type of discount to apply: `fixed` or `percent`.
- `customizable` (boolean): Set `true` to enable custom options for this product in the admin panel.
- `description` (string): A long form description of the product. May have HTML or other markup.
- `images` (object): List of images depicting the bundle.
  - `caption` (string): A brief description of the image, intended for display as a caption or alt text.
  - `file` (object): An object representing the image's source file.
    - `data` (file): Set or overwrite file data. Use the following format when writing a file from binary data (for example an image): `data[$binary]=<base64 encoded binary daya>`.
    - `filename` (string): Optional file name.
- `meta_description` (string): Page description used for search engine optimization purposes.
- `meta_keywords` (string): Page keywords used for search engine optimization purposes.
- `meta_title` (string): Page title used to override product name in storefronts.
- `Options` (object): Options that allow for variations of the base product. If the option is part of a variant or `required=true`, an option value must be set for the product to be added to a cart.
  - `name` (string): Human-friendly name of the option.
  - `input_hint` (string): Some brief hint text to help the user understand this option.
  - `input_type` (string): Type of user input to display for this option in a storefront. Can be `text`, `textarea`, `select`, `multi_select`, `file` or `multi_file`. `select` is ideal for dropdown or radio selectors, and `multi_select` is ideal for checkboxes. A maximum of 10 files can be uploaded by the user, and there are no restrictions on file type.
  - `parent_id` (objectId): Specifies another option ID that affects visibility of this option. The option will only appear when one of the `parent_value_ids` is selected.
  - `parent_value_ids` (array of objectId): The ids of parent option values that will make the option appear if selected.
  - `price` (currency): Extra price for the option, added to the product's `price`/`sale_price`. If the option is part of a variant, the variant's `price`/`sale_price` will override this value.
  - `subscription` (boolean): Set `true` to indicate the option's values specify a subscription billing interval. In this case, option values must have a `subscription_interval` of `monthly`, `yearly`, `weekly` or `daily`.
  - `values` (object): List of possible values for this option.
    - `name` (string): Human-friendly name of the option value.
    - `description` (string): A brief description of the option value, intended for displaying to customers.
    - `images` (object): One or more images depicting the option value.
      - `caption` (string): A brief description of the image, intended for display as a caption or alt text.
      - `file` (object): An object representing the image's source file.
        - `data` (file): Set or overwrite file data. Use the following format when writing a file from binary data (for example an image): `data[$binary]=<base64 encoded binary daya>`.
        - `filename` (string): Optional file name.
    - `price` (currency): Extra price added to the product's `price`/`sale_price` if the option value is selected. Overrides option `price`.
    - `shipment_weight` (float): Extra weight added to the product's `shipment_weight` if the option value is selected. The unit should match the store's default as configured in general settings.
    - `subscription_interval` (string): When product `type=subscription`, this is the billing interval used when this option value is selected. Can be `monthly`, `yearly`, `weekly` or `daily`.
    - `subscription_interval_count` (int): When product `type=subscription`, this number multiplies `subscription_interval` to determine the billing frequency when this option is selected. For example, to make a subscription bill every 2 weeks, set `subscription_interval=weekly` and `subscription_interval_count=2`.
    - `subscription_trial_days` (int): When product `type=subscription`, refers to a number of days offered as a trial before an invoice is issued.
  - `variant` (boolean): Set `true` to generate variants including this option.
- `prices` (object): Set price rules to use when conditions match the customer's account group or product quantity in a cart.
  - `price` (currency): Set price to apply when conditions are met.
  - `account_group` (string): Customer account group as a condition to apply price.
  - `quantity_max` (int): Maximum quantity as a condition to apply price.
  - `quantity_min` (int): Minimum quantity as a condition to apply price.
- `quantity_inc` (int): Specifies a quantity multiple the product must be sold in.
- `quantity_min` (int): Minimum quantity of the product that can be sold at once.
- `sale` (boolean): Set `true` to mark the product "on sale" and to use `sale_price` when the product is added to a cart.
- `sale_price` (currency): Sale price used to override list price when `sale=true`.
- `shipment_dimensions` (object): Product dimensions when packed for shipping. Typically used by 3rd party carriers in box packing algorithms to optimize shipping costs.
  - `height` (float): Height of the product in `unit`.
  - `length` (float): Length of the product in `unit`.
  - `unit` (string): Either `in` (inches) or `cm` (centimeters).
  - `width` (float): Width of the product in `unit`.
- `shipment_location` (string): ID of location from `/settings/shipping/locations`. If specified, shipping is calculated from this origin. Otherwise, the store default location will be used.
- `shipment_package_quantity` (float): If specified, shipping is calculated using this as the maximum number of items per package. Otherwise, Swell assumes any quantity fits into a single package.
- `shipment_prices` (object): Product shipping price rules to override default shipping rules.
  - `service` (string): Shipping service required for this rule to apply.
  - `account_group` (string): Customer group required for this rule to apply.
  - `country` (string): Shipping country required for this rule to apply.
  - `fee_amount` (currency): Fixed amount to add when rule is applied. Only applicable when `fee_type=fixed`.
  - `fee_percent` (float): Percentage of the shipping price to add when rule is applied. Only applicable when `fee_type=percent`.
  - `fee_type` (string): Type of fee to apply in addition to `price`, either `fixed` or `percent`.
  - `package_quantity` (int): Maximum package quantity when rule is applied.
  - `price` (currency): Shipping price when rule is applied.
  - `state` (string): Shipping state required for this rule to apply.
  - `total_max` (currency): Maximum order subtotal for this rule to apply.
  - `total_min` (currency): Minimum order subtotal for this rule to apply.
  - `weight_max` (float): Maximum order item weight for this rule to apply.
  - `weight_min` (float): Minimum order item weight for this rule to apply.
  - `zip` (string): Shipping zip/postal code required for this rule to apply.
- `shipment_weight` (float): If specified, shipping is calculated using this weight. Otherwise, Swell assumes 1 lb/oz/kg — depending on store's default weight unit.
- `sku` (string): Stock keeping unit (SKU) used to track inventory in a warehouse.
- `stock_tracking` (boolean): Set `true` to enable stock tracking this product.
- `subscription_interval` (string): The default billing interval when this product is used as a subscription plan. Can be `monthly`, `yearly`, `weekly` or `daily`.
- `subscription_interval_count` (int): Multiplier when combined with `subscription_interval`. For example, to make a subscription bill every 2 weeks, set `subscription_interval=weekly` and `subscription_interval_count=2`.
- `subscription_trial_days` (int): Number of days offered as a free trial before the customer is billed. If a subscription is canceled by the last day of the trial period, an invoice won't be issued.
- `tags` (array of string): List of arbitrary tags typically used as metadata to improve search results or associate custom behavior with a product.
- `type` (string): Implies the ordering and fulfillment options available for the product. Can be `standard`, `subscription`, `bundle`, or `giftcard`. A `standard` product is a physical item that will be shipped to a customer.
- `up_sells` (object): List of products to display as up-sells on a product detail page.
  - `product_id` (objectId): The id of the up-sell product.
- `variable` (boolean): Set `true` to generate variants for this product in the admin panel.

## Example request

**Update a product**

`PUT /products/:id`

**cURL**

```bash
$ curl https://api.swell.store/products/5ca24abb9c077817e5fe2b36 \
  -u store-id:secret-key \
  -d price=9.99 \
  -X PUT
```

**Node**

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

await swell.put('/products/{id}', {
  id: '5ca24abb9c077817e5fe2b36',
  price: 19.98,
  // use $set to override values
  $set: {
    options: [
      ...
    ],
  },
});
```

**PHP**

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

$swell->put('/products/{id}', [
  'id' => '5ca24abb9c077817e5fe2b36',
  'price' => 19.98,
  // use $set to override values
  '$set' => [
    'options' => [
      ...
    ],
  ],
]);
```

## Example response

```json
{
  "id": "5ca24abb9c077817e5fe2b36",
  "active": true,
  "attributes": {},
  "currency": "USD",
  "date_created": "2019-04-01T00:00:00.000Z",
  "date_updated": "2019-04-01T00:00:00.000Z",
  "delivery": "shipment",
  "description": null,
  "images": [
    {
      "id": "5ca24abb9c077817e5fe2b37",
      "file": {
        "id": "5ca24abb9c077817e5fe2b38",
        "date_uploaded": "2019-04-02T00:26:23.399Z",
        "length": 66764,
        "md5": "99194f53bfdea832553e7fa8ae8fd80f",
        "content_type": "image/png",
        "url": "http://cdn.swell.store/test/5ca24abb9c077817e5fe2b36/99194f53bfdea832553e7fa8ae8fd80f",
        "width": 940,
        "height": 600
      }
    }
  ],
  "meta_description": null,
  "meta_title": null,
  "name": "T-Shirt",
  "options": [
    {
      "id": "5ca24ab32599d4179c24a624",
      "name": "Size",
      "variant": true,
      "required": true,
      "values": [
        {
          "id": "5ca24ad59c077817e5fe2ba3",
          "name": "Small"
        },
        {
          "id": "5ca24ad59c077817e5fe2ba4",
          "name": "Medium"
        },
        {
          "id": "5ca24ad59c077817e5fe2ba5",
          "name": "Large"
        }
      ]
    }
  ],
  "price": 9.99,
  "slug": "swell-t-shirt",
  "stock_level": 0,
  "stock_status": "available",
  "stock_tracking": true,
  "tags": [],
  "type": "standard"
}
```
