Add product to the cart

Ecwid.Cart.addProduct()

This method allows you to add a new product position to the shopping cart. It accepts two arguments: product and callback:

  • product argument accepts either product ID or a product object with extended details including product ID, quantity, selected options, or subscription product settings.

  • callback allows you to execute any app logic that depends on the result of the operation, for example, updating the checkout UI or refreshing the cart. Use callback to ensure that your logic runs only after the product has been successfully added.

When you use this method, it first checks the product by specified ID for stock and variations. If the selected options match one of the variations, Ecwid will check its stock first. If you don't have enough stock to add a product, Ecwid adds the available quantity. If the product is out of stock, nothing is added to the shopping cart.

Code example:

var product = {
  id: 10,
  quantity: 3,
  options: {
    "Size": "L"
  },
  recurringChargeSettings: { 
    recurringInterval: "month",
    recurringIntervalCount: 1,
    },
  callback: function(success, product, cart) {
    // ...  
  }
}

Ecwid.Cart.addProduct(product);

product argument fields

Field
Type
Description

id

number

Internal product ID.

quantity

number

Quantity of the product that will be added to the cart.

options

object options

Map of the selected product options (option name as a key and option value as a value).

selectedPrice

string

Selected price for the product with the parameter "nameYourPriceEnabled": true. If specified for a product with "nameYourPriceEnabled": false, the product will be added with the default price.

recurringChargeSettings

string

Defines subscription product settings if needed.

callback

string

Defines the callback function.

Code example for options object:

"options": {  
    "Size": "M",  
    "Print_number": "9"  
}

recurringChargeSettings

Field
Type
Description

recurringInterval

string

Charge recurring interval. Supported values: DAY, WEEK, MONTH, YEAR.

recurringIntervalCount

number

Charge recurring interval. Supported values: for DAY - 1 (daily), for WEEK - 1 (weekly), 2 (biweekly), for MONTH - 1 (monthly), 3 (quarterly), for YEAR - 1 (annually).

callback

Callback function receives 4 arguments: success, product, cart, error

Name
Type
Description

success

boolean

Indicates operation status. If true - the product was added to the shopping cart.

product

object product

product object after the operation.

cart

object cart

cart object after the operation.

error

string

If success is false, contains error message.

Last updated

Was this helpful?