JSMA cart

Overview

The jsma cart provides access to the current session cart object. Whenever a user creates a session, a cart object will be created lazily (it will be created when first time accessing JSMA carts method carts.current).

What can JSMA cart do for me?

You can use the JSMA cart to manage a users shopping session and facilitate the transaction.

Index

Usage

writing data

Here is an example on how to write data to a JSMA cart:

function updateShipping(countryCode)
{
  //calculate current shipping cost
  var cart = carts.current;
    
  //retrieve country code
  var country = countryCode;
  if (!country) {
    country = cart.shippingAddress.countryCode;
  }
    
  if (!country) {
    cart.setShipping(0);
    return;
  }

  [...]
}

reading data

Here is an example on how to read data from a JSMA cart:

CartApi.prototype.doUpdate = function ()
{
  var result = {
    status : "OK",
    id : null,
    price : 0,
    shipping : 0,
    tax : 0,
    items : [],
    voucher : null
  };
    
  var cart = carts.current;
    
  //core values for cart
  result.id = cart.uuid;
  result.amount = cart.price;
  result.shipping = cart.shipping;
  result.tax = cart.tax;
  result.attributes = {};
    
  //add cart attributes
  cart.flexAttributes.forEach ( function ( key ) {
    result.attributes[key] = cart.get(key);
  });

  [...]
}

Methods

The JSMA cart provides the following methods:

cartItem cart.addItem(product_id, [project], [custom_price])

Adds an item to the items list of JSMA cartItem

Returns the new cartItem.

Example

// we either update a cart item
if (request.parameter('replaceItem')) {
  cartItem = carts.current.updateItem(productId, request.parameter('project'), request.parameter('replaceItem'));
}
// or add a new one.
else {
  cartItem = carts.current.addItem(productId, request.parameter('project')); // => cartItem
}

Parameter int product_id (required)

The id of the product

Parameter project (optional)

The project object

Parameter int custom_price (optional)

The price in cent

cartItem cart.addItemFromOrderItem(item_uuid)

Adds a JSMA orderItem (specified by uuid) to the items list of JSMA cartItem

Returns the new cartItem.

Example

else if (itemAction === "addToCart")
{
  var item = cart.addItemFromOrderItem(itemUuid); // => cartItem
  
  result.status = "OK";
  result.statusMessage = "Item " + item + " added to cart";

  return JSON.stringify(result);    
}

Parameter item_uuid (required)

cartItem cart.addVoucher(token, value, title)

If a valid voucher token was given as first argument this method will add a voucher to the cart and return a corresponding JSMA cartItem

Example

...
cart.addVoucher('easter25', 495, 'Oster-Rabatt!') // => cartItem
...

Parameter token (required)

A token string.

Parameter value (optional)

Price in cents. Must be a positive integer.

Parameter title (optional)

Title of the CartItem

cart.flexAttributes

Returns the flex attributes associated with the cart

Example

//add cart attributes
cart.flexAttributes.forEach ( function ( key ) {
  result.attributes[key] = cart.get(key);
});

string cart.get(key)

Returns the value of key as string or null if no value is set under the key.

Example

if (cart.get("ibe2.shippingMethod") === "STORE") {
  regionShipping = parseInt(portal.get("shipping.store.price"));
}

Parameter key:string (required)

The key to be looked up in the flexAttributes of the cart.

boolean cart.has(key)

Returns a boolean value depending on whether key is present for a given cart

Example

...
cart.has("ibe2.shippingMethod") // => true
...

Parameter key:string (required)

The key to be checked in the flexAttributes of the cart.

address cart.invoiceAddress

Returns the invoice address as JSMA address.

Example

...
cart.invoiceAddress // => JSMA address
...

cartItem[] cart.items

Returns a list of cartItems.

Example

var items = cart.items; // => cartItems
var productValue = 0;

for (var i=0;i<items.length;i++)
{
  var item = items[i];
    
  productValue += Math.round(item.price*100.0);
    
  if (item.type === "product")
  {
    var product = item.product;
    var shippingGroup = product.get("shipping.group");
    
  [...]
    shipping = Math.max(shipping , parseInt(regionShipping));    
  }
}  

string cart.paymentMethod

Returns the paymentMethod field of a cart.

Example

...
cart.paymentMethod // => 'paypal'
...


string cart.paypalPurchaseUrl(return_url, cancel_url)

Returns the URL to purchase a cart with paypal.

Example

CartApi.prototype.doSubmitOrderPaypal = function ()
{
  var cart = carts.current;
    
  var paypalCancelUrl = options.cancelUrl;
  var paypalReturnUrl = options.returnUrl;
    
  var paypalPurchaseUrl = cart.paypalPurchaseUrl(paypalReturnUrl, paypalCancelUrl);

  var result = {
    status : "OK",
    paypalPurchaseUrl : paypalPurchaseUrl
  };
    
  return result;
};

Parameter return_url (required)

The URL to which the user will be redirected after finishing the transaction on the paypal website.

Parameter cancel_url (required)

The URL to which the user will be redirected when cancelling the transaction on the paypal website.

portal cart.portal

Returns the JSMA portal of the cart.

Example

...
cart.portal // => 'Rhino Staging'
...

int cart.price

Returns the price of the cart in cents

Example

...
cart.price // => 1995
...


void cart.removeItem(uuid)

Removes the cartItem specified by uuid from the cart.

Example

...
cart.removeItem('A_UUID_OF_A_CARTITEM');
...

Parameter uuid (required)

A uuid from the list of JSMA cartItems of the cart.

cart.remove(key)

Removes the key from the flexAttributes of the cart

Example

...
cart.remove('ibe2.shippingMethod');
...

Parameter key:string (required)

The key to be removed from the flexAttributes of the cart.

cart.set(key, value)

Sets the value of a specified key for a given cart.

Example

CartApi.prototype.doSubmitFlexAttributes = function () 
{
  var cart = carts.current;

  [...]

  if (attributes[cart.uuid]) {
    for (var key in attributes[cart.uuid]) {
      cart.set(key, attributes[cart.uuid][key]);
    }
  }
}

Parameter key:string (required)

The key to be set in the flexAttributes of the cart.

Parameter value:string (required)

The value to be set for the key


void cart.setPaymentMethod(payment_mode)

Sets the paymentMethod of the cart to the specified value.

Example

CartApi.prototype.doSubmitOrderPaypalSuccess = function ()
{    
  var cart = carts.current;
    
  cart.setPaymentMethod("paypal");

  [...]
}

Parameter payment_mode (required)

void cart.setShipping(cent)

Sets the shipping cost of the cart.

Example

...
cart.setShipping(495);
...

Parameter cent (required)

bool cart.setShippingMode(shipping_mode)

Sets the shippingMode to the desired value. Returns true if update was successful.

Example

...
cart.setShippingMode('shipping_normal_xl'); // => true
...

Parameter shipping_mode (required)

May be either of

  • store
  • shipping_normal
  • shipping_normal_xl
  • shipping_express
  • shipping_express_xl

int cart.shipping

Returns the shipping cost in cents.

Example

...
cart.shipping // => 495
...

address cart.shippingAddress

Returns the shipping address as JSMA address.

Example

...
cart.shippingAddress // => JSMA address
...

int cart.tax

Returns the included VAT of the cart in cents.

Example

...
cart.tax // => 205
...

cartItem cart.updateItem(product_id, project, uuid)

Replace a cartItem by uuid with a new one.

Example

// we either update a cart item
if (request.parameter('replaceItem')) {
  cartItem = carts.current.updateItem(productId, request.parameter('project'), request.parameter('replaceItem')); 
}
// or add a new one.
else {
  cartItem = carts.current.addItem(productId, request.parameter('project'));
}

Parameter int product_id (required)

The id of the product

Parameter project (required)

The project object

Parameter uuid (required)

The uuid of the item

string cart.uuid

Returns the cart's uuid.

Example

CartApi.prototype.doSubmitFlexAttributes = function ()
{
  var cart = carts.current;

  [...]

  if (attributes[cart.uuid]) {
    for (var key in attributes[cart.uuid]) {
      cart.set(key, attributes[cart.uuid][key]);
    }
  }
}