Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 accessing JSMA carts method carts.current).

Info

What can JSMA cart do for me?

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

...

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

Code Block
languagejs
linenumberstrue
# TODOfunction 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:

Code Block
languagejs
linenumberstrue
# TODOCartApi.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:

...

Adds an item to the items list of JSMA cartItem

Returns the new cartItem.

...

Code Block
languagejs
linenumberstrue
// we either update a cart item
if (request.parameter('replaceItem')) {
  cartItem = carts.current.
cart.addItem(1);
...

...

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.

...

Code Block
languagejs
linenumberstrue
...else if (itemAction === "addToCart")
{
  var item = cart.addItemFromOrderItem(1itemUuid); // => 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

Code Block
languagejs
linenumberstrue
...
cart.addVoucher('A_VALID_VOUCHER_TOKEN'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

...

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

string cart.get(key)

Accesses a flex attribute field for the given parameter keyReturns the value of key as string or null if no value is set under the key.

Example

Code Block
languagejs
linenumberstrue
...if (cart.get(key)
...

Parameter key (required)

"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 true if a cart has a flex attribute for the given parameter key, otherwise false.a boolean value depending on whether key is present for a given cart

Example

Code Block
languagejs
linenumberstrue
...
cart.has(key)
"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

Code Block
languagejs
linenumberstrue
...
cart.invoiceAddress // => JSMA address
...

cartItem[] cart.items

...

Code Block
languagejs
linenumberstrue
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.
cart.items
...product;
    var shippingGroup = product.get("shipping.group");
    
  [...]
    shipping = Math.max(shipping , parseInt(regionShipping));    
  }
}  

string cart.paymentMethod

Returns the paymentMethod field of a cart.

...

Code Block
languagejs
linenumberstrue
CartApi.prototype.doSubmitOrderPaypal = function ()
{
  var cart = carts.current;
    
  var paypalCancelUrl = options.cancelUrl;
  var paypalReturnUrl = options.returnUrl;
    
  var paypalPurchaseUrl = cart.paypalPurchaseUrl('http://my-website.com/success', 'http://my-website.com/cart')
...paypalReturnUrl, paypalCancelUrl);

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

Parameter return_url (required)

...

portal cart.portal

Returns the JSMA portal of the cart.

Example

Code Block
languagejs
linenumberstrue
...
cart.portal // => 'Rhino Staging'
...

int cart.price

Returns the price of the cart in cents

...

Parameter uuid (required)

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

cart.

...

remove(key

...

)

Sets a flex attribute with the given parametersRemoves the key from the flexAttributes of the cart

Example

Code Block
languagejs
linenumberstrue
...
cart.set(key, value)remove('ibe2.shippingMethod');
...

Parameter key:string (required)

...

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

cart.

...

set(key, value)

Removes a flex attribute for given parameter keySets the value of a specified key for a given cart.

Example

Code Block
languagejs
linenumberstrue
CartApi.prototype.doSubmitFlexAttributes = function () 
{
  var cart = carts.current;

 cart.remove(key)
...

Parameter key (required)

 [...]

  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)

...

Code Block
languagejs
linenumberstrue
CartApi.prototype.doSubmitOrderPaypalSuccess = function ()
{    
  var cart = carts.current;
    
  cart.setPaymentMethod('invoice'"paypal");

  [...]
}

Parameter payment_mode (required)

...

Returns the shipping address as JSMA address.

Example

Code Block
languagejs
linenumberstrue
...
cart.shippingAddress // => JSMA address
...

...

Code Block
languagejs
linenumberstrue
// we either update a cart item
if (request.parameter('replaceItem')) {
  cartItem = carts.current.
cart.updateItem(product_idproductId, request.parameter('project'), uuid)
...

...

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.

...

Code Block
languagejs
linenumberstrue
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]);
    }
  }
}