Methods
Commerce exposes a range of methods to create and process orders from within your own code.
In the most extreme case these methods (and the JS API Events) can be used to build a custom user interface, but they can also be used for smaller tasks like adding or removing items from the cart.
Commerce.cart
Commerce.cart.add
Adds a product or multiple products to the cart.
This can be called while Commerce is loading and the product will added to the cart once Commerce re-enters the ready state.
Signature
- Commerce.cart.add( product: String ): Promise
- Commerce.cart.add( product: Object ): Promise
- Commerce.cart.add( products: Array ): Promise
Arguments
product: String
The product to be added to the cart.'-LrpxAwMv1XZo8vyd0r8'
product: Object
The product to be added to the cart.{ id: '-LrpxAwMv1XZo8vyd0r8', // required, the unique id of the product quantity: 1, // optional, will be 1 when not provided fields: { ... } // optional, initial field values for the product }
products: Array
An array of product ids (as outlined above).[ '-LrpxAwMv1XZo8vyd0r8', '-LrpxdXvDmcRXLjnLVnG' ]
An array of product objects (as outlined above).
[ { id: '-LrpxAwMv1XZo8vyd0r8', quantity: 4, fields: { ... } }, { id: '-LrpxdXvDmcRXLjnLVnG', quantity: 2, fields: { ... } } ]
Response
Promise
Success
{ ok: true, cart: { ... } }
Error
{ ok: false }
Examples
// Add a single product Commerce.cart.add( '-LrpxAwMv1XZo8vyd0r8' ) // Add a single product Commerce.cart.add( { id: '-LrpxAwMv1XZo8vyd0r8' } ) // Add a product in a particular quantity Commerce.cart.add( { id: '-LrpxAwMv1XZo8vyd0r8', quantity: 3 } ) // Add a product with a field selection Commerce.cart.add( { id: '-LtXyVbjyRrML3AHD8Zx', fields: { color: 'Red' } ) // Add a product with multiple field selections Commerce.cart.add( { id: '-LtXyVbjyRrML3AHD8Zx', fields: { color: 'Red', size: 'Large' } } ) // Add multiple products at once Commerce.cart.add( [ '-LrpxAwMv1XZo8vyd0r8', '-LrpxdXvDmcRXLjnLVnG', '-LtXyVbjyRrML3AHD8Zx' ] ) // Add multiple products at once Commerce.cart.add( [ { id: '-LrpxAwMv1XZo8vyd0r8' }, { id: '-LrpxdXvDmcRXLjnLVnG' }, { id: '-LtXyVbjyRrML3AHD8Zx' } ] ) // Add multiple products at once with different quantities and field selections Commerce.cart.add( [ { id: '-LrpxAwMv1XZo8vyd0r8', quantity: 3 }, { id: '-LtXyVbjyRrML3AHD8Zx', fields: { color: { value: 'Red' }, size: { value: 'Large' } } }, { id: '-LtXyVbjyRrML3AHD8Zx', quantity: 2, fields: { color: { value: 'Green' }, size: { value: 'Small' } } } ] )
Commerce.cart.clear
Clears all the items from the cart.
Signature
- Commerce.cart.clear(): Promise
Response
Promise
Success
{ ok: true, cart: { discounts: [], items: {}, price: { saving: { amount: 0, formatted: '$0.00' }, subtotal: { amount: 0, formatted: '$0.00' }, total: { amount: 0, formatted: '$0.00' } }, quantity: 0, valid: false } }
Error
{ ok: false, message: 'Unable to clear the cart' }
Commerce.cart.decrement
Decrements the quantity of an item in the cart.
Signature
- Commerce.cart.decrement( id: String, amount?: Number ): Promise
Arguments
id: String
The id of the item to decrement the quantity of.
Note: This is the unique id of the item in the cart, not the product id.'-LsUMTyd4XEDGZ9F_SGx'
amount: Number (optional)
The amount to decrement the quantity by.
If not specified it defaults to1
.3
Response
Promise
Success
{ ok: true, cart: { ... } }
Error
// When no id is supplied { ok: false, message: 'You must supply the id of an item that is in the cart' } // When the id is suppled incorrectly { ok: false, message: 'The cart item id must be a String' } // When the id cannot be found in the cart { ok: false, message: 'The specified id could not be found' } // When an incorrect decrement is specified { ok: false, message: 'You cannot decrement by a negative number' } // When a float decrement is specified { ok: false, message: 'You can only decrement by an integer' }
Commerce.cart.get
Retrieves either the entire cart (including all totals and discounts), or a specific item from the cart.
When retrieving the entire cart, the valid
property will be false
if any items are invalid.
Signature
- Commerce.cart.get( id?: String ): Object
Arguments
- id: String (optional)
The id of the cart item to retrieve.'-LsUMTyd4XEDGZ9F_SGx'
- id: String (optional)
Response
Object
Returns the entire cart when no id is supplied.
{ discounts: [], items: { '-LsUMTyd4XEDGZ9F_SGx': { discounts: [], fields: [], price: { subtotal: { amount: 9, formatted: '$9.00' }, total: { amount: 9, formatted: '$9.00' }, unit: { amount: 9, formatted: '$9.00' } }, product: { ... }, quantity: 1 }, '-LtX_BcA23Xv7Ols96kz': { discounts: [], fields: [], price: { subtotal: { amount: 3.75, formatted: '$3.75' }, total: { amount: 3.75, formatted: '$3.75' }, unit: { amount: 3.75, formatted: '$3.75' } }, product: { ... }, quantity: 1 }, }, price: { saving: { amount: 0.0, formatted: '$0.00' } subtotal: { amount: 12.75, formatted: '$12.75' } total: { amount: 12.75, formatted: '$12.75' } }, quantity: 2, valid: true }
Returns only the matching item when an id is supplied.
{ discounts: [], fields: [], price: { subtotal: { amount: 9, formatted: '$9.00' }, total: { amount: 9, formatted: '$9.00' }, unit: { amount: 9, formatted: '$9.00' } }, product: { ... }, quantity: 1 }
null
- Returns null when no matching item is found.
null
- Returns null when no matching item is found.
Commerce.cart.increment
Increments the quantity of an item in the cart.
Signature
- Commerce.cart.increment( id: String, amount?: Number ): Promise
Arguments
id: String
The id of the item to increment the quantity of.
Note: This is the unique id of the item in the cart, not the product id.'-LsUMTyd4XEDGZ9F_SGx'
amount: Number (optional)
The amount to increment the quantity by.
If not specified it defaults to1
.3
Response
Promise
Success
{ ok: true, cart: { ... } }
Error
// When no id is supplied { ok: false, message: 'You must supply the id of an item that is in the cart' } // When the id is suppled incorrectly { ok: false, message: 'The cart item id must be a String' } // When the id cannot be found in the cart { ok: false, message: 'The specified id could not be found' } // When an incorrect increment is specified { ok: false, message: 'You cannot increment by a negative number' } // When a float increment is specified { ok: false, message: 'You can only increment by an integer' }
Commerce.cart.modify
Modifies an item in the cart.
Signature
- Commerce.cart.modify( id: String, path: String, value: ? ): Promise
Arguments
id: String
The id of the cart item to modify'-LsUMTyd4XEDGZ9F_SGx'
path: String
The path to modify on the item.'quantity' 'fields.size' 'fields.color'
value: ?
The value to set at the specified path.7 'Large' 'Red'
Response
Promise
Success
{ ok: true, cart: { ... } }
Error
// When no id is supplied { ok: false, message: 'You must supply the id of an item that is in the cart' } // When the id is suppled incorrectly { ok: false, message: 'The cart item id must be a String' } // When no path is supplied { ok: false, message: 'You must supply the path to modify' } // When the path is suppled incorrectly { ok: false, message: 'The path must be a String' } // When the id cannot be found in the cart { ok: false, message: 'The specified id could not be found' } // When a quantity modification is less than 1 { ok: false, message: 'The quantity cannot be less than 1' } // When a quantity modification is not an integer { ok: false, message: 'The quantity must be an integer' }
Examples
// Modify the quantity of an item Commerce.cart.modify( '-LsUMTyd4XEDGZ9F_SGx', 'quantity', 8 ) // Modify a field called 'size' Commerce.cart.modify( '-LsUMTyd4XEDGZ9F_SGx', 'fields.size', 'Large' ) // Modify a field called 'color' Commerce.cart.modify( '-LsUMTyd4XEDGZ9F_SGx', 'fields.color', 'Red' )
Commerce.cart.remove
Removes a product from the cart.
Signature
- Commerce.cart.remove( id: String ): Promise
Arguments
- id: String
The id of the item to remove from the cart.
Note: This is the unique id of the item in the cart, not the product id.'-LsUMTyd4XEDGZ9F_SGx'
- id: String
Response
Promise
Success
{ ok: true, cart: { ... } }
Error
// When no id is supplied { ok: false, message: 'You must supply the id of an item that is in the cart' } // When the id is suppled incorrectly { ok: false, message: 'The cart item id must be a String' } // When the id cannot be found in the cart { ok: false, message: 'The specified id could not be found' }
Commerce.config
Commerce.config.get
Retrieves a configuration value from the local store.
Commerce config values are set via the embed
or the Admin.
Signature
- Commerce.config.get( path?: String ): Object
Arguments
- path: String (optional)
The path of the config value to retrieve.'cart.authenticators'
- path: String (optional)
Response
Object
Returns all config values when no path is supplied.
{ cart: { authenticators: [ 'email' ], messages: { 'checkout-success': '', 'shipping-address': '' , 'shipping-note': '' }, notes: [ { id: 'delivery', label: 'Delivery Instructions' }, { id: 'order', label: 'Order Notes' } ], preferences: { 'billingaddress-enabled': true, 'lastname-required': false, 'phone-required': false, 'shippingnote-enabled': true } }, currency: 'USD', embed: { key: '...' }, environment: 'live', expose: 'Commerce', instance: '64cc70ba48', organization: 'VlWw8jlSw2', payments: { currencies: [ 'AUD', 'CAD', 'EUR', 'GBP', 'JPY', 'USD' ], gateways: [ 'stripe', 'paypal' ], paypal: { enabled: true }, stripe: { enabled: true, publishableKey: '...' } }, shipping: { countries: [ 'AU', 'US' ] } }
Returns only the matching config value when a path is supplied.
[ 'email' ]
null
- Returns null when no matching config value is found.
null
- Returns null when no matching config value is found.
Commerce.coupons
Commerce.coupons.add
Adds a coupon to the active coupons.
Signature
- Commerce.coupons.add( coupon: String ): Promise
- Commerce.coupons.add( coupons: Array ): Promise
Arguments
coupon: String
The case-sensitive string coupon to add to the active coupons.'fitnessfirst'
coupons: Array
An array of case-sensitive string coupons to add to the active coupons.[ 'fitnessfirst', 'WOAH-MULTIPLE-COUPONS!' ]
Response
Promise
Success
// When at least one of the supplied codes is found { ok: true, coupons: [ ... ], message: 'Promo code found' }
Error
// When unspported arguments are provided { ok: false, coupons: [ ... ], message: 'Invalid promo code' } // When none of the supplied codes are found { ok: false, coupons: [ ... ], message: 'Promo code not found' }
Commerce.coupons.clear
Clears all active coupons.
Signature
- Commerce.coupons.clear(): Promise
Response
Promise
- Success
{ ok: true, coupons: [], message: 'Promo codes cleared' }
- Success
Commerce.coupons.get
Retrieves the active coupons.
Signature
- Commerce.coupons.get(): Array
Response
Array
- Returns all active coupon codes.
[ { applied: true, saving: { amount: 73.9, formatted: "$73.90" }, coupon: 'fitnessfirst' } ]
- Returns all active coupon codes.
Commerce.coupons.remove
Removes a coupon from the active coupons.
Signature
- Commerce.coupons.remove( coupon: String ): Promise
- Commerce.coupons.remove( coupons: Array ): Promise
Arguments
coupon: String
The case-sensitive string coupon to remove from the active coupons.'fitnessfirst'
coupons: Array
An array of case-sensitive string coupons to remove from the active coupons.[ 'fitnessfirst', 'WOAH-MULTIPLE-COUPONS!' ]
Response
Promise
Success
// When at least one of the supplied codes is found { ok: true, coupons: [ ... ], message: 'Promo code removed' }
Error
// When unspported arguments are provided { ok: false, coupons: [ ... ], message: 'Invalid promo code' } // When none of the supplied codes are found { ok: false, coupons: [ ... ], message: 'Promo code not found' }
Commerce.currency
Commerce.currency.get
Retrieves the active currency.
Signature
- Commerce.currency.get(): String
Response
String
- Returns the ISO 4217 currency code for the active currency.
'AUD'
- Returns the ISO 4217 currency code for the active currency.
Commerce.currency.list
Lists all currency codes that are enabled.
Signature
- Commerce.currency.list(): Array
Response
Array
- Returns an array of the ISO 4217 currency codes.
[ 'AUD', 'CAD', 'EUR', 'GBP', 'JPY', 'USD' ]
- Returns an array of the ISO 4217 currency codes.
Commerce.currency.set
Sets the active currency.
Signature
- Commerce.currency.set( code: String ): Promise
Arguments
- code: String
The ISO 4217 currency code to set.
This must be one of the codes found in theCommerce.currency.available()
array'USD'
- code: String
Response
Promise
Success
{ ok: true, code: '...' }
Error
{ ok: false, code: '...' }
Commerce.discounts
Commerce.discounts.get
Retrieves the raw discounts that are available and provides an indication if they have been applied and the total saving generated.
The currently applied discount effects can be seen in context within the result of Commerce.cart.get()
.
Signature
- Commerce.discounts.get(): Object
Response
- Object
- Returns an array of all available discounts.
[ { applied: true, conditions: [ ... ], effects: [ ... ], id: '2oi32hg29992223', saving: { amount: 3, formatted: '$3.00' } }, { applied: false, conditions: [ ... ], effects: [ ... ], id: '4280hsdok3k20dj', saving: { amount: 0, formatted: '$0.00' } }, ... ]
- Returns an array of all available discounts.
- Object
Commerce.giftcard
Commerce.giftcard.set
Sets a gift card against the order.
Signature
- Commerce.giftcard.set( giftcard: String ): Promise
Arguments
- giftcard: String
The gift card to add to the order.'SCM2EGAI1HAKT3YDG1GN'
- giftcard: String
Response
Promise
Success
// When the gift card is found { ok: true, giftcard: { balance: { amount: 25, formatted: '$25.00' }, code: 'SCM2EGAI1HAKT3YDG1GN', currency: 'AUD', value: { amount: 25, formatted: '$25.00' } } }
Error
// When the gift card is not found { ok: false, message: 'Gift Card is incorrect' } // When an unspported argument type is provided { ok: false, message: 'Gift Card must be a String' } // When the gift card is in a different currency { ok: false, message: "Gift Card requires currency of '{CURRENCY}'" } // When an error is encountered confirming the gift card { ok: false, message: 'Error loading Gift Card' }
Commerce.giftcard.clear
Clears the gift card from the order.
Signature
- Commerce.giftcard.clear(): Promise
Response
Promise
- Success
{ ok: true }
- Success
Commerce.giftcard.get
Retrieves the gift card against the order.
Signature
- Commerce.giftcard.get(): Object
Response
Object
- Returns the active gift card.
{ balance: { amount: 25, formatted: '$25.00' }, code: 'SCM2EGAI1HAKT3YDG1GN', currency: 'AUD', value: { amount: 25, formatted: '$25.00' } }
- Returns the active gift card.
null
- Returns null when there is no active gift card.
null
- Returns null when there is no active gift card.
Commerce.inventory
Commerce.inventory.get
Retrieves the complete details for a product from the inventory.
Signature
- Commerce.inventory.get( id: String ): Promise
- Commerce.inventory.get( product: Object ): Promise
Arguments
id: String
The id of the product to retrieve.'-LrpxAwMv1XZo8vyd0r8'
product: Object
The product to retrieve from the inventory.{ id: '-LrpxAwMv1XZo8vyd0r8' // required, the id of the product }
Response
Promise
Success
// When the product is found { ok: true, product: { id: '-LrpxAwMv1XZo8vyd0r8', ... }, group: { ... } // group if the product is in one } // When the product is missing { error: { type: 'missing', title: 'Product missing', message: 'This product does not exist or has been removed from sale' }, ok: true, product: { id: '-LrpxAwMv1XZo8vyd0r8', ... } } // When the product is unavailable { error: { type: 'unavailable', title: 'Product unavailable', message: 'This product is currently unavailable' }, ok: true, product: { id: '-LrpxAwMv1XZo8vyd0r8', ... } } // When the product is unpriced { error: { type: 'unpriced', title: 'Product unpriced', message: 'This product does not have a price set' }, ok: true, product: { id: '-LrpxAwMv1XZo8vyd0r8', ... } } // When the product is out of stock { error: { type: 'unavailable', title: 'Product out of stock', message: 'This product is currently out of stock' }, ok: true, product: { id: '-LrpxAwMv1XZo8vyd0r8', ... } } // When the product is malformed { error: { type: 'malformed', title: 'Product malformed', message: 'This product does not exist or has been removed from sale' }, ok: true, product: { id: '-LrpxAwMv1XZo8vyd0r8', ... } }
Error
// When the product is supplied incorrectly { ok: false, message: 'The product to retrieve is defined incorrectly' } // When the product id has not been supplied { ok: false, message: 'You must supply the product id to retrieve' } // When the product id is the wrong type { ok: false, message: 'The product id must be a String' }
Commerce.inventory.modify
Modifies an item in place.
Signature
- Commerce.inventory.modify( product: String, path: String, value: ? ): Promise
- Commerce.inventory.modify( item: Object, path: String, value: ? ): Promise
Arguments
product: String
The product to be modified.'-LrpxAwMv1XZo8vyd0r8'
item: Object
The item to modify.{ product: { id: '-LrpxAwMv1XZo8vyd0r8', ... }, ... }
path: String
The path to modify on the item.'quantity' 'fields.size' 'fields.color'
value: ?
The value to set at the specified path.7 'Large' 'Red'
Response
Promise
Success
{ ok: true, item: { ... } }
Error
// When no path is supplied { ok: false, message: 'You must supply the path to modify' } // When the path is suppled incorrectly { ok: false, message: 'The path must be a String' } // When a quantity modification is less than 1 { ok: false, message: 'The quantity cannot be less than 1' } // When a quantity modification is not an integer { ok: false, message: 'The quantity must be an integer' }
Examples
// Retrieves a product with a calculated quantity Commerce.inventory.modify( '-LsUMTyd4XEDGZ9F_SGx', 'quantity', 8 ) // Retrieves a product with a field selection for 'size' Commerce.inventory.modify( '-LsUMTyd4XEDGZ9F_SGx', 'fields.size', 'Large' ) // Retrieves a product with a field selection for 'color' Commerce.inventory.modify( '-LsUMTyd4XEDGZ9F_SGx', 'fields.color', 'Red' ) // Modifies an existing product instance Commerce.inventory.modify( { product: { id: '-LsUMTyd4XEDGZ9F_SGx', ... }, ... }, 'fields.color', 'Red' )
Commerce.order
Commerce.order.get
Retrieves the complete details for the order.
The order is automatically cleared once the transaction is successful, if you need to access the order details after that point you can pass 'completed'
.
Signature
- Commerce.order.get(): Object
- Commerce.order.get( 'completed' ): Object
Response
- Object
Prior to a quote being selected, and if there is an error.
{ addresses: { ... }, cart: { ... }, error: { message: '...', help: '...' }, modifiers: null, notes: [ ... ], payment: { ... }, price: null, quote: { selected: null, options: [] }, shipping: null, user: { ... }, valid: false }
When the order is valid and can be purchased.
{ addresses: { ... }, cart: { ... }, modifiers: [ ... ], notes: [ ... ], payment: { ... }, price: { ... }, quote: { selected: '...', options: [ ... ] }, shipping: { ... }, user: { ... }, valid: true }
- Object
Commerce.order.purchase
Purchases the order when the payment method is set to Stripe.
To purchase via PayPal the customer must use the PayPal button (see: Commerce.util.paypal.mount
).
Signature
- Commerce.order.purchase(): Promise
Response
Promise
Success
{ ok: true, message: 'Thank you for your purchase. ...', invoice: { ... }, order: { ... } }
Error
// When there are no items in the cart { ok: false, message: 'You must add at least one item to the cart', help: 'Use Commerce.cart.add, https://docs.static.tools/commerce/api/methods.html#commerce-cart-add' } // When the cart is in an invalid state // (to rectify this ensure that no cart items have an error) { ok: false, message: 'You must ensure all items in the cart are valid', help: 'Check Commerce.cart.get, https://docs.static.tools/commerce/api/methods.html#commerce-cart-get' } // When shipping is required and a shipping address has not been specified { ok: false, message: 'You must specify a shipping address', help: 'Use Commerce.order.addresses.set, https://docs.static.tools/commerce/api/methods.html#commerce-order-addresses-set' } // When a billing address is required and has not been specified { ok: false, message: 'You must specify a billing address', help: 'Use Commerce.order.addresses.set, https://docs.static.tools/commerce/api/methods.html#commerce-order-addresses-set' } // When contact details for the account have not been specified { ok: false, message: 'You must specify contact details', help: 'Use Commerce.user.details.set, https://docs.static.tools/commerce/api/methods.html#commerce-user-details-set' } // When a quote for the order has not yet been requested { ok: false, message: 'You must fetch a quote', help: 'Use Commerce.order.quote.fetch, https://docs.static.tools/commerce/api/methods.html#commerce-order-quote-fetch' } // When a quote for the order has not yet been selected { ok: false, message: 'You must select quote', help: 'Use Commerce.order.quote.select, https://docs.static.tools/commerce/api/methods.html#commerce-order-quote-select' } // When the payment method has not been set { ok: false, message: 'You must select a payment method', help: 'Use Commerce.order.payment.set, https://docs.static.tools/commerce/api/methods.html#commerce-order-payment-set' } // When the payment method is Stripe but valid details have not been provided { ok: false, message: 'You must provide valid card details', help: 'Use Commerce.order.payment.set, https://docs.static.tools/commerce/api/methods.html#commerce-order-payment-set' } // When the payment method is PayPal { ok: false, message: 'PayPal payments must be triggered by the customer via the PayPal Button', help: 'Use Commerce.util.paypal.mount, https://docs.static.tools/commerce/api/methods.html#commerce-util-paypal-mount' }
Commerce.order.addresses
Commerce.order.addresses.get
Retrieves addresses from the order.
Signature
- Commerce.order.addresses.get( type?: String ): Object
Arguments
- type: String (optional)
The type of address to retrieve, eithershipping
orbilling
.'shipping'
- type: String (optional)
Response
Object
Returns all addresses when no type is supplied.
{ billing: { city: 'Westerville', country: 'US', fullname: 'Lorrie Glick', id: '-LtYPEmHSeo78Sz8CV5e', line1: '4595 Quilly Lane', line2: null, postcode: '43081', state: 'OH' }, shipping: { city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', id: '-LrM6xWAfLmnuuqYRIbl', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' } }
Returns only the matching address when a type is supplied.
{ city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', id: '-LrM6xWAfLmnuuqYRIbl', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' }
null
- Returns null when no matching address is found.
null
- Returns null when no matching address is found.
Examples
// Retrieves the shipping address let shippingAddress = Commerce.addresses.get( 'shipping' ) // Retrieves the billing address let billingAddress = Commerce.addresses.get( 'billing' )
Commerce.order.addresses.set
Sets an address against the order.
An address can be set either directly or by id reference.
Signature
- Commerce.order.addresses.set( type: String, id: String ): Promise
- Commerce.order.addresses.set( type: String, address: Object ): Promise
Arguments
type: String
The type of address being set, eithershipping
orbilling
.'shipping'
id: String
The id of the existing address to set.'-LrM6xWAfLmnuuqYRIbl'
address: Object
The address as perCommerce.user.addresses.add
.{ city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' }
Response
Promise
Success
{ ok: true, address: { city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', id: '-LrM6xWAfLmnuuqYRIbl', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' }, addresses: { billing: null, shipping: { city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', id: '-LrM6xWAfLmnuuqYRIbl', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' } } }
Error
// When validation fails { ok: false, address: { ... }, type: '...', errors: { ... }, message: '...' } // When the address add fails { ok: false, message: 'Error adding address' } // When the country of the address isn't enabled as a shipping destination { ok: false, address: { ... }, message: 'Cannot ship to the specified country' }
Commerce.order.notes
Commerce.order.notes.clear
Clears the order notes.
Signature
- Commerce.order.notes.clear(): Promise
Response
Promise
- Success
{ ok: true, notes: [ { id: 'delivery', label: 'Delivery Instructions', value: '' }, { id: 'order', label: 'Order Notes', value: '' } ] }
Commerce.order.notes.get
Retrieves notes from the order.
Signature
- Commerce.order.notes.get(): Array
- Commerce.order.notes.get( id: String ): Object
Arguments
- id: String (optional)
The id of the note to retrieve.'delivery'
- id: String (optional)
Response
Array
- Returns all notes when no id is supplied.
[ { id: 'delivery', label: 'Delivery Instructions', value: 'Please leave at the front door' }, { id: 'order', label: 'Order Notes', value: 'Please match Dye Lot 7625' } ]
- Returns all notes when no id is supplied.
Object
- Returns only the matching note when an id is supplied.
{ id: 'delivery', label: 'Delivery Instructions', value: 'Please leave at the front door' }
- Returns only the matching note when an id is supplied.
null
- Returns null when no matching note is found.
null
- Returns null when no matching note is found.
Commerce.order.notes.set
Sets a note against the order.
Signature
- Commerce.order.notes.set( id: String, value: String ): Promise
Arguments
id: String
The id of the note to set.'delivery'
value: String
The value of the note to set.'Please leave at the front door'
Response
Promise
Success
{ ok: true, notes: [ ... ] }
Error
// When the id has not been supplied { ok: false, message: "The note 'id' is required" } // When the value has not been supplied { ok: false, message: "The note 'value' is required" } // When the id is the wrong type { ok: false, message: "The note 'id' must be a String" } // When the value is the wrong type { ok: false, message: "The note 'value' must be a String" } // When the note id does not exist { ok: false, message: "The note id '...' does not exist" }
Examples
// Sets a 'delivery instructions' note Commerce.order.notes.set( 'delivery', 'Please leave at the front door' ) // Sets an 'order' note Commerce.order.notes.set( 'order', 'Please match Dye Lot 7625' )
Commerce.order.payment
Commerce.order.payment.get
Retrieves the payment details for the order.
Signature
- Commerce.order.payment.get(): Object
Response
- Object
// When the payment method is PayPal { details: null, method: 'paypal' } // When the payment method is Stripe // and payment details have been set { details: { ... }, method: 'stripe' }
- Object
Commerce.order.payment.set
Sets the payment details for the order.
Signature
- Commerce.order.payment.set( payment: Object ): Promise
Arguments
payment: Object
The object representing the customers payment details.{ // Either 'paypal' or 'stripe' method: 'stripe', // When the method is 'paypal' the details will be null // When the method is 'stripe' the details must be // - When anonymous, use Commerce.util.stripe.save() to automatically set this value // - When registered, this should be the 'id' of a saved card from Commerce.user.cards.get() details: { ... } }
This can also be supplied partially and will merge into the existing details.
{ method: 'stripe' }
{ details: 'card_1F2oWzFBCj3vUdLmJHPxS2o2' }
Response
Promise
Success
{ ok: true, payment: { method: '...', details: { ... } } }
Error
// When no method has been set { ok: false, message: "You must supply a 'method'" } // When an invalid payment method is specified { ok: false, message: "'...' is not a valid payment method" } // When payment details are set for PayPal { ok: false, message: 'You cannot set payment details when using PayPal' } // When an invalid Stripe card id is supplied { ok: false, message: 'The stripe payment details must be a String card id' } // When the Stripe card referenced does not exist { ok: false, message: `The stripe card '${payment.details}' could not be found` } // When the Stripe details are set incorrectly for an anonymous user { ok: false, message: 'Use Commerce.util.stripe.save() to set the payment details for an anonymous order' }
Commerce.order.quote
Commerce.order.quote.fetch
Retrieves a quote for the order from our cloud processing.
The options returned by the quote factor in tax rates and shipping.
The cheapest available option is automatically selected, to change the selection use Commerce.order.quote.select
Signature
- Commerce.order.quote.fetch(): Promise
Response
Promise
Success
{ ok: true, options: [ { id: '27169130-4993-4fe7-b3fa-bb8b13b793f1', modifiers: [ { name: 'Shipping', price: { amount: 11.95, formatted: '$11.95' }, service: { name: 'Australia Post Express Parcel', provider: 'auspost' }, type: 'shipping' }, { included: true, name: 'GST (10%) Inc.', price: { amount: 0.06, formatted: '$0.06' }, rate: 0.1, type: 'tax' } ], price: { subtotal: { amount: 7, formatted: '$7.00' }, total: { amount: 18.95, formatted: '$18.95' } }, shipping: { details: { costs: { cost: { item: 'Express Post', cost: '11.95' } }, delivery_time: 'Guaranteed Next Business Day within the Express Post network (If posted on any business day Monday to Friday in accordance with the conditions set out on the item).', service: 'Express Post', total_cost: '11.95' }, key: 'auspost-domestic-express-parcel', name: 'Australia Post Express Parcel', price: { amount: 11.95, formatted: '$11.95' }, provider: 'auspost' } }, { id: '05424b6e-b57f-4d98-b90f-7197455c98d0' , modifiers: [ { name: 'Shipping', price: { amount: 18.95, formatted: '$18.95' }, service: { name: 'Australia Post Satchel (5kg)', provider: 'auspost' }, type: 'shipping' }, { included: true, name: 'GST (10%) Inc.', price: { amount: 0.06, formatted: '$0.06' }, rate: 0.1, type: 'tax' } ], price: { subtotal: { amount: 7, formatted: '$7.00' }, total: { amount: 25.95, formatted: '$25.95' } }, shipping: { key: 'auspost-domestic-regular-satchel-5kg', name: 'Australia Post Satchel (5kg)', price: { amount: 18.95, formatted: '$18.95' }, provider: 'auspost' } } ] }
Error
// When there are no items in the cart { ok: false, help: 'Use Commerce.cart.add, https://docs.static.tools/commerce/api/methods.html#commerce-cart-add', message: 'You must add at least one item to the cart' } // When the cart is in an invalid state // (to rectify this ensure that no cart items have an error) { ok: false, help: 'Check Commerce.cart.get, https://docs.static.tools/commerce/api/methods.html#commerce-cart-get', message: 'You must ensure all items in the cart are valid' } // When shipping is required and a shipping address has not been specified { ok: false, help: 'Use Commerce.order.addresses.set, https://docs.static.tools/commerce/api/methods.html#commerce-order-addresses-set', message: 'You must specify a shipping address' }
Commerce.order.quote.select
Selects a quote ready for purchase.
Signature
- Commerce.order.quote.select( id: String ): Promise
Arguments
- id: String
The id of the quote to select.'3f25aa19-0eff-4493-bdc0-0e8ef72f28e4'
- id: String
Response
Promise
Success
{ ok: true, option: { ... } }
Error
// When no id is supplied { ok: false, message: 'You must supply the id of a quote' } // When the id is suppled incorrectly { ok: false, message: 'The quote id must be a String' } // When the quote id cannot be found { ok: false, message: `The id '...' could not be found in the quote` }
Commerce.referrer
Commerce.referrer.clear
Clears the active referrer.
Signature
- Commerce.referrer.clear(): Promise
Response
Promise
- Success
{ ok: true }
- Success
Commerce.referrer.get
Retrieves the active referrer.
Signature
- Commerce.referrer.get(): String
Response
String
- Returns the active referrer.
'statictools'
- Returns the active referrer.
null
- Returns null when there is no active referrer.
null
- Returns null when there is no active referrer.
Commerce.referrer.set
Sets the active referrer.
Signature
- Commerce.referrer.set( referrer: String ): Promise
Arguments
- referrer: String
The string used to represent a referrer.'statictools'
- referrer: String
Response
Promise
Success
{ ok: true, referrer: '...' }
Error
{ ok: false, message: 'The referrer must be a String' }
Commerce.user
Commerce.user.get
Retrieves all information for the current user.
This includes their addresses, cards and details if available.
Signature
- Commerce.user.get(): Object
Response
- Object
{ addresses: { '-LtYPEmHSeo78Sz8CV5e': { city: 'Westerville', country: 'US', fullname: 'Lorrie Glick', line1: '4595 Quilly Lane', line2: null, postcode: '43081', state: 'OH' }, '-LrM6xWAfLmnuuqYRIbl': { city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' } }, auth: { anonymous: false, uid: 'lOE8XjQ9LRQRqEQlXIJd7gyX7Qw2' }, details: { email: 'lorrie@glickclack.com', firstname: 'Lorrie', fullname: 'Lorrie Glick', lastname: 'Glick', phone: '8159445464' } }
- Object
Commerce.user.login
Authenticates the user providing access to their persistent account.
Signature
- Commerce.user.login( email: String, password: String ): Promise
Arguments
email: String
The email address to login.'noreply@static.tools'
password: String
The password for the account.'correct.horse.battery.staple'
Response
Promise
Success
{ ok: true, user: { auth: { anonymous: false, uid: '...' }, details: { ... } } }
Error
// When no authenticators are enabled { ok: false, message: 'Login is disabled' } // When no arguments are provided { ok: false, message: 'You must supply login details' } // When the Email authenticator is disabled { ok: false, message: 'Login with Email is disabled' } // When unspported arguments are provided { ok: false, message: 'Unsupported login format' } // When email login validation fails { ok: false, email: '...', errors: { ... }, message: '...' } // When a login is attempted while already logged in { ok: false, message: 'You cannot login while already logged in' } // When the user does not exist { ok: false, message: 'User could not be found' } // When the password is incorrect { ok: false, message: 'Email and Password combination not found' } // When email/password login fails { ok: false, message: 'There was an error with your login' }
Commerce.user.logout
De-authenticates the user and resets to a new 'anonymous' user.
Signature
- Commerce.user.logout(): Promise
Response
Promise
Success
{ ok: true, user: { auth: { anonymous: true, uid: '...' } } }
Error
// When the user is not currently signed in { ok: false, message: 'You are not signed in' }
Commerce.user.lookup
Checks if the specified email address has an account.
Signature
- Commerce.user.lookup( email: String ): Promise
Arguments
- email: String
The email address to check.'noreply@static.tools'
- email: String
Response
Promise
Success
// The 'account' value indicates if the account exists { ok: true, account: true }
Error
// When validation fails { ok: false, email: '...', errors: { ... }, message: '...' } // When lookup is attempted while logged in { ok: false, message: 'You cannot lookup an account while logged in' }
Commerce.user.recover
Sends an account recovery email to the specified email address.
Signature
- Commerce.user.recover( email: String ): Promise
Arguments
- email: String
The email address to send a recovery email.'noreply@static.tools'
- email: String
Response
Promise
Success
{ ok: true, email: '...' }
Error
// When validation fails { ok: false, email: '...', errors: { ... }, message: '...' } // When a recovery is attempted while already logged in { ok: false, message: 'You cannot recover an account while already logged in' } // When the recovery fails { ok: false, message: 'Unable to send recovery email' }
Commerce.user.register
Signature
- Commerce.user.register( email: String, password: String ): Promise
Arguments
email: String
The email address to register.'noreply@static.tools'
password: String
The password for the account.'correct.horse.battery.staple'
Response
Promise
Success
{ ok: true, user: { auth: { anonymous: false, uid: '...' }, details: { ... } } }
Error
// When the Email authenticator is disabled { ok: false, message: 'Register is disabled' } // When validation fails { ok: false, email: '...', errors: { ... }, message: '...' } // When register is attempted while logged in { ok: false, message: 'You cannot register while logged in' } // When the account already exists { ok: false, message: 'There is already an account with this email address' } // When registration fails { ok: false, message: 'There was an error with your registration' }
Commerce.user.addresses
The addresses methods allow you to manipulate the addresses that are stored against the current user account.
Commerce.user.addresses.add
Adds an address to the user's account.
The address is persistent when the user is logged in, otherwise it will be temporary.
If the user is anonymous the type
parameter must be supplied.
Signature
- Commerce.user.addresses.add( address: Object, type?: String ): Promise
Arguments
- address: Object
The address to add to the user's account.{ city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' }
- type: String (optional)
The type of address is required when saving to an anonymous account.'shipping' 'billing'
- address: Object
Response
Promise
Success
{ ok: true, address: { city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', id: '-LrM6xWAfLmnuuqYRIbl', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' }, addresses: { '-LtYPEmHSeo78Sz8CV5e': { city: 'Westerville', country: 'US', fullname: 'Lorrie Glick', line1: '4595 Quilly Lane', line2: null, postcode: '43081', state: 'OH' }, '-LrM6xWAfLmnuuqYRIbl': { city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' } } }
Error
// When the 'type' is not supplied for an anonymous user { ok: false, message: "You must supply a 'type' when the user is anonymous" } // When the 'type' supplied is incorrect { ok: false, message: "The 'type' must be either 'shipping' or 'billing'" } // When validation fails { ok: false, address: { ... }, type: '...', errors: { ... }, message: '...' } // When the address add fails { ok: false, message: 'Error adding address' }
Commerce.user.addresses.get
Retrieves addresses from the user's account.
Signature
- Commerce.user.addresses.get( id?: String ): Object
Arguments
- id: String (optional)
The id of the address to retrieve.'-LrM6xWAfLmnuuqYRIbl'
- id: String (optional)
Response
Object
Returns all addresses when no id is supplied.
{ '-LtYPEmHSeo78Sz8CV5e': { city: 'Westerville', country: 'US', fullname: 'Lorrie Glick', line1: '4595 Quilly Lane', line2: null, postcode: '43081', state: 'OH' }, '-LrM6xWAfLmnuuqYRIbl': { city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' } }
Returns only the matching address when an id is supplied.
{ city: 'Manhattan', country: 'US', fullname: 'Inez Ellis', id: '-LrM6xWAfLmnuuqYRIbl', line1: '1989 Settlers Lane', line2: null, postcode: '10016', state: 'NY' }
null
- Returns null when no matching address is found.
null
- Returns null when no matching address is found.
Commerce.user.addresses.remove
Removes an address from the user's account.
Signature
- Commerce.user.addresses.remove( id: String ): Promise
Arguments
- id: String
The id of the address to remove.'-LrM6xWAfLmnuuqYRIbl'
- id: String
Response
Promise
Success
{ ok: true, addresses: { ... } }
Error
// When the supplied address id does not exist { ok: false, message: 'You must supply the id of an existing address' } // When the address remove fails { ok: false, message: 'Error removing address' }
Commerce.user.cards
The Commerce.user.cards
methods allow you to manipulate the cards that are stored against the current user account.
To save a card to the user's account see the following methods:
Commerce.user.cards.fetch
Retrieves all cards from the user's account.
Signature
- Commerce.user.cards.fetch(): Promise
Response
Promise
Success
{ ok: true, cards: [ { brand: 'Visa', country: 'US', id: 'card_1F2oWzFBCj3vUdLmJHPxS2o2', last4: '4242', ... }, { brand: 'Visa', country: 'US', id: 'card_1FZqiiFBCj3vUdLm6GkISxUX', last4: '1234', ... } ] }
Error
{ ok: false, message: 'Error loading cards' }
Commerce.user.cards.remove
Removes a card from the user's account.
Signature
- Commerce.user.cards.remove( id: String ): Promise
Arguments
- id: String
The id of the card to remove.'card_1F2oWzFBCj3vUdLmJHPxS2o2'
- id: String
Response
Promise
Success
{ ok: true, cards: [ { brand: 'Visa', country: 'US', id: 'card_1FZqiiFBCj3vUdLm6GkISxUX', last4: '1234', ... } ] }
Error
// When no id is supplied { ok: false, message: 'You must supply the id of the card to remove' } // When the card could not be removed { ok: false, message: 'Error removing card' }
Commerce.user.details
Commerce.user.details.get
Retrieves the contact details for the current user.
Signature
- Commerce.user.details.get(): Object
Response
- Object
All details only appear if they have been provided.{ email: 'lorrie@glickclack.com', firstname: 'Lorrie', fullname: 'Lorrie Glick', lastname: 'Glick', phone: '8159445464' }
- Object
Commerce.user.details.set
Sets the contact details for the current user.
Signature
- Commerce.user.details.set( details: Object ): Promise
Arguments
- details: Object
The contact details for the user.{ email: 'lorrie@glickclack.com', // required for anonymous users, ignored for authed users firstname: 'Lorrie', lastname: 'Glick', // optional / required when set by config preferences phone: '8159445464' // optional / required when set by config preferences }
- details: Object
Response
Promise
Success
{ ok: true, details: { email: "lorrie@glickclack.com", firstname: "Lorrie", fullname: "Lorrie Glick", lastname: "Glick", phone: "8159445464" } }
Error
// When validation fails { ok: false, details: { ... }, errors: { ... }, message: '...' } // When saving the user details fails { ok: false, details: { ... }, message: 'Error saving details' }
Commerce.user.orders
Commerce.user.orders.fetch
Retrieves all previous orders from the user's account.
Signature
- Commerce.user.orders.fetch(): Promise
Response
Promise
Success
{ ok: true, orders: [ { id: 'CG-10011', ... }, { id: 'CG-10010', ... }, ... ] }
Error
{ ok: false, message: 'Error loading orders' }
Commerce.util.address
Commerce.util.address.fetch
Retrieves the full details of an address suggestion.
Signature
- Commerce.util.address.fetch( suggestion: Object ): Promise
Arguments
- suggestion: Object
The address to fetch.{ address: '30 Collins Street, Melbourne VIC, Australia', id: 'ChIJPZN_nnRD1moRl0XLrdGAj9g' }
- suggestion: Object
Response
Promise
- Success
{ ok: true, details: { address: '30 Collins Street, Melbourne VIC, Australia', line1: '30 Collins Street', city: 'Melbourne', state: 'Victoria', country: 'Australia', postcode: '3000' } }
- Success
Commerce.util.address.lookup
Provides suggestions for the supplied address.
Signature
- Commerce.util.address.lookup( address: String, iso2: String ): Promise
Arguments
address: String
The address to check.'30 Collins St'
iso2: String
An iso2 country code.'AU'
Response
Promise
- Success
{ ok: true, addresses: [ { "address": "30 Collins Street, Melbourne VIC, Australia", "id": "ChIJPZN_nnRD1moRl0XLrdGAj9g" }, { "address": "30 Collins Street, Annandale NSW, Australia", "id": "ChIJKQPooSawEmsRf4rnp6fDxr0" }, { "address": "30 Collins Street, Mentone VIC, Australia", "id": "ChIJ3b3K2dNt1moRx-j1dSO5TBQ" }, { "address": "30 Collins Street, Hobart TAS, Australia", "id": "ChIJrbT1DoN1bqoRSyMqLlgtNwE" }, { "address": "30 Collins Street, Clayfield QLD, Australia", "id": "ChIJUxhKaVdYkWsRTwB1wkmGHl0" } ] }
- Success
Commerce.util.country
Commerce.util.country.codes
Retrieves an array of all iso2 country codes.
Signature
- Commerce.util.country.codes(): Array
Response
- Array
[ 'AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', ... ]
- Array
Commerce.util.country.name
Retrieves the country name for the specified iso2 code.
Signature
- Commerce.util.country.name( iso2: String ): String
Arguments
- iso2: String
An iso2 country code.'AU'
- iso2: String
Response
- String
'Australia'
- String
Commerce.util.country.regions
Retrieves the regions for the specified iso2 code.
Signature
- Commerce.util.country.regions( iso2: String ): String
Arguments
- iso2: String
An iso2 country code.'AU'
- iso2: String
Response
- String
[ 'Australian Capital Territory' 'New South Wales' 'Northern Territory' 'Queensland' 'South Australia' 'Tasmania' 'Victoria' 'Western Australia' ]
- String
Commerce.util.paypal
Commerce.util.paypal.isReady
Indicates if the PayPal button is mounted and ready.
Signature
- Commerce.util.paypal.isReady(): Boolean
Response
- Boolean
When the PayPal button is ready.
true
When the PayPal button is not ready.
false
- Boolean
Commerce.util.paypal.load
Loads the dependencies required for mounting a PayPal payment button.
This is provided to allow the dependencies to be preloaded, otherwise they will load automatically the first time Commerce.util.paypal.mount
is used.
Signature
- Commerce.util.paypal.load(): Promise
Response
Promise
Success
{ ok: true }
Error
// When PayPal is not configured as a payment method { ok: false, message: 'PayPal is disabled' } // When the PayPal dependencies fail to load { ok: false, urls: [ ... ] // The urls that failed to load }
Commerce.util.paypal.mount
Mounts the PayPal button to the DOM Element specified.
Signature
- Commerce.util.paypal.mount( element: String / DOMElement ): Promise
Arguments
- element: String / DOMElement
The element into which the PayPal button should mount itself.// When selecting a class '.my-paypal-button-holder' // When selecting an id '#my-paypal-button-holder'
- element: String / DOMElement
Response
Promise
Success
{ ok: true, element: ... }
Error
// When no element is supplied { ok: false, message: 'You must specify the element for the mount' } // When the element cannot be found { ok: false, message: 'The element could not be found' }
Commerce.util.stripe
Commerce.util.stripe.clear
Clears the Stripe input.
Signature
- Commerce.util.stripe.clear(): Object
Response
Object
When the input is cleared.
{ ok: true, input: { ... } }
When there is no Stripe input.
{ ok: false, message: 'There is no Stripe input' }
Commerce.util.stripe.isReady
Indicates if the Stripe input is mounted and ready.
Signature
- Commerce.util.stripe.isReady(): Boolean
Response
- Boolean
When the Stripe input is ready
true
When the Stripe input is not ready
false
- Boolean
Commerce.util.stripe.load
Loads the dependencies required for mounting a Stripe input.
This is provided to allow the dependencies to be preloaded, otherwise they will load automatically the first time Commerce.util.stripe.mount
is used.
Signature
- Commerce.util.stripe.load(): Promise
Response
Promise
Success
{ ok: true }
Error
// When Stripe is not configured as a payment method { ok: false, message: 'Stripe is disabled' } // When the Stripe dependencies fail to load { ok: false, urls: [ ... ] // The urls that failed to load }
Commerce.util.stripe.mount
Mounts the Stripe input to the DOM Element specified.
Signature
- Commerce.util.stripe.mount( element: String / DOMElement ): Promise
Arguments
- element: String / DOMElement
The element into which the Stripe input should mount itself.// When selecting a class '.my-stripe-input-holder' // When selecting an id '#my-stripe-input-holder'
- element: String / DOMElement
Response
Promise
Success
{ ok: true, element: ... , input: { ... } }
Error
// When Stripe is not configured as a payment method { ok: false, message: 'Stripe is disabled' } // When no element is supplied { ok: false, message: 'You must specify the element for the mount' } // When the element cannot be found { ok: false, message: 'The element could not be found' }
Commerce.util.stripe.save
Signature
- Commerce.util.stripe.save(): Promise
Response
Promise
Success
{ ok: true }
Error
// When there is no Stripe input { ok: false, message: 'There is no stripe input' } // When there is an error during token creation return { ok: false, message: '...' } // When adding the card details fails { ok: false, message: 'Error adding card' }
Commerce.util.stripe.unmount
Unmounts the Stripe input from the DOM.
Signature
- Commerce.util.stripe.unmount(): Object
Response
Object
When the input is unmounted.
{ ok: true, input: { ... } }
When there is no Stripe input.
{ ok: false, message: 'There is no Stripe input' }