Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

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 product_id (required)

Parameter project (optional)

Parameter custom_price (optional)

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);
});

cart.get(key)

Accesses a flex attribute field for the given parameter key

Example

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

Parameter key (required)

cart.has(key)

Returns true if a cart has a flex attribute for the given parameter key, otherwise false.

Example

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

Parameter key (required)

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.set(key, value)

Sets a flex attribute with the given parameters

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 (required)

Parameter value (required)

cart.remove(key)

Removes a flex attribute for given parameter key.

Example

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

Parameter key (required)

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 product_id (required)

Parameter project (required)

Parameter uuid (required)

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]);
    }
  }
}
  • No labels