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. |
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; } [...] } |
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); }); [...] } |
The JSMA cart provides the following methods:
Adds an item to the items list of JSMA cartItem
Returns the new cartItem.
// 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 } |
The id of the product
The project object
The price in cent
Adds a JSMA orderItem (specified by uuid) to the items list of JSMA cartItem
Returns the new cartItem.
else if (itemAction === "addToCart") { var item = cart.addItemFromOrderItem(itemUuid); // => cartItem result.status = "OK"; result.statusMessage = "Item " + item + " added to cart"; return JSON.stringify(result); } |
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
... cart.addVoucher('easter25', 495, 'Oster-Rabatt!') // => cartItem ... |
A token string.
Price in cents. Must be a positive integer.
Title of the CartItem
Returns the flex attributes associated with the cart
//add cart attributes cart.flexAttributes.forEach ( function ( key ) { result.attributes[key] = cart.get(key); }); |
Returns the value of key as string or null if no value is set under the key.
if (cart.get("ibe2.shippingMethod") === "STORE") { regionShipping = parseInt(portal.get("shipping.store.price")); } |
The key to be looked up in the flexAttributes of the cart.
Returns a boolean value depending on whether key is present for a given cart
... cart.has("ibe2.shippingMethod") // => true ... |
The key to be checked in the flexAttributes of the cart.
Returns the invoice address as JSMA address.
... cart.invoiceAddress // => JSMA address ... |
Returns a list of cartItems.
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)); } } |
Returns the paymentMethod field of a cart.
... cart.paymentMethod // => 'paypal' ... |
Returns the URL to purchase a cart with paypal.
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; }; |
The URL to which the user will be redirected after finishing the transaction on the paypal website.
The URL to which the user will be redirected when cancelling the transaction on the paypal website.
Returns the JSMA portal of the cart.
... cart.portal // => 'Rhino Staging' ... |
Returns the price of the cart in cents
... cart.price // => 1995 ... |
Removes the cartItem specified by uuid from the cart.
... cart.removeItem('A_UUID_OF_A_CARTITEM'); ... |
A uuid from the list of JSMA cartItems of the cart.
Removes the key from the flexAttributes of the cart
... cart.remove('ibe2.shippingMethod'); ... |
The key to be removed from the flexAttributes of the cart.
Sets the value of a specified key for a given cart.
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]); } } } |
The key to be set in the flexAttributes of the cart.
The value to be set for the key
Sets the paymentMethod of the cart to the specified value.
CartApi.prototype.doSubmitOrderPaypalSuccess = function () { var cart = carts.current; cart.setPaymentMethod("paypal"); [...] } |
Sets the shipping cost of the cart.
... cart.setShipping(495); ... |
Sets the shippingMode to the desired value. Returns true if update was successful.
... cart.setShippingMode('shipping_normal_xl'); // => true ... |
May be either of
Returns the shipping cost in cents.
... cart.shipping // => 495 ... |
Returns the shipping address as JSMA address.
... cart.shippingAddress // => JSMA address ... |
Returns the included VAT of the cart in cents.
... cart.tax // => 205 ... |
Replace a cartItem by uuid with a new one.
// 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')); } |
The id of the product
The project object
The uuid of the item
Returns the cart's uuid.
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]); } } } |