JSMA cartItem

Overview

The JSMA CartItem provides access to one item in a cart (JSMA cart) object.


What can JSMA cartItem do for me?

Using the JSMA cartItem you can access and modify individual items in a users cart.

Index

Usage

writing data

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

if (request.parameter('savedproject')) {
  cartItem.set('ibe2.savedproject', request.parameter('savedproject'));  
}

if (productData.price) { 
  cartItem.setPrice(productData.price);
}
    
if (productData.amount) {
  cartItem.setAmount(productData.amount);
}
    
if (productData.description) {
  cartItem.setDescription(productData.description);
}
    
if (productData.article) {
  cartItem.set("article", productData.article);  
}

reading data

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

//cart items
cart.items.forEach ( function ( cartItem ) {

if (cartItem.type === "product") {

var product = new PathProduct(cartItem.product.name);

var description = cartItem.description;

//default to product name1
if (!description) {
description = product.name1 + " " + product.name2;
}

//handle preview images
var previewImageId = null;
if (cartItem.previewImages && cartItem.previewImages.length > 0) {
previewImageId = cartItem.previewImages[0].uuid;
}
else {
previewImageId = product.productImage;
}

//handle edit url
var editUrl = product.editUrl + "?piced=" + cartItem.projectId + "&cartItem=" + cartItem.uuid;

//handle attributes
var attributes = {};
//add cart item attributes
cartItem.flexAttributes.forEach ( function ( key ) {
attributes[key] = cartItem.get(key);
});

[...]
});

Methods

The JSMA cartItem provides the following methods:

int cartItem.amount

Returns the quantity of an item.

Example

else if (type === "voucher")
{
  amountChooser.append(
    $("<span>")
      .addClass("item-amount")
      .text(cartItem.amount)          
  );
}

string cartItem.description

Returns the description field of a cartItem.

Example

else if (type === "item")
{
  cartItemLabel.append(
    $("<span>")
      .addClass("item-description")
      .text(cartItem.description)          
  );
}

cartItem.flexAttributes

Accesses the flex attributes of a JSMA cartItem

Example

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

string cartItem.get(key)

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

Example

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

Parameter key:string (required)

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

boolean cartItem.has(key)

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

Example

if (cartItem.has('product.type')) {
  result.type = cartItem.get('product.type');
}

Parameter key:string (required)

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

file[] cartItem.previewImages

Returns a list of JSMA file for further access to the previewImages of an item.

Example

if (cartItem.previewImages && cartItem.previewImages.length > 0) {
  previewImageId = cartItem.previewImages[0].uuid;
}
else {
  previewImageId = product.productImage;
}

int cartItem.price

Returns the price of an item in cents.

Example

//create item row
var cartRow = $("<tr>")
  .attr("data-item-type", cartItem.type)
  .attr("data-item-uuid", cartItem.uuid)
  .attr("data-item-price", cartItem.price)
  .addClass("cart-table-row");

cartRow.append(
  (previewEnabled) ? $("<td>").addClass("item-preview").append(previewImage) : "",
  (nameEnabled) ? $("<td>").addClass("item-name").html(cartItem.display) : "",
  (amountEnabled) ? $("<td>").addClass("item-amount").html(amountChooser) : "",
  (singlePriceEnabled) ? $("<td>").addClass("item-single-price").html(singlePriceText) : "",
  (priceEnabled) ? $("<td>").addClass("item-price").html(formatMoney(cartItem.price) + " " + cart.currency) : "",
  (actionsEnabled) ? $("<td>").addClass("item-actions").html(itemOptions) : ""
);

product cartItem.product

Returns a JSMA product for the current item.

Example

//add edit button
itemOptions.append(
  $("<a>")
    .addClass("edit-cart-item btn btn-primary btn-block")
    .attr("href", cartItem.product.editPath + "?piced=" + cartItem.projectId)
    .append(
  $("<i>").addClass("fa fa-pencil"),
    " Als Vorlage verwenden"
  )
);

file cartItem.project

Returns a JSMA file for the saved project of the current item.

Returns null if no project is associated with the item.

Example

var result = {
  projectId : cartItem.project.uuid,
  projectLocation : cartItem.project.path,
  status : "OK"
};

string cartItem.projectId

Returns the uuid of the file that is associated as project for the current item.

Example

//add edit button
itemOptions.append(
  $("<a>")
    .addClass("edit-cart-item btn btn-primary btn-block")
    .attr("href", cartItem.product.editPath + "?piced=" + cartItem.projectId)
    .append(
  $("<i>").addClass("fa fa-pencil"),
    " Als Vorlage verwenden"
  )
);

cartItem.set(key, value)

Sets a key, value pair as a flex attribute to the current item.

Example

if (request.parameter('savedproject')) {
  cartItem.set('ibe2.savedproject', request.parameter('savedproject'));
}

Parameter key (required)

Parameter value (required)

cartItem.remove(key)

Removes the key from the flexAttributes of the cartItem

Example

cartItem.remove('ibe2.savedproject');

Parameter key:string (required)

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

void cartItem.setAmount([qty])

Sets the quantity if the current item to the desired value.

Example

if (productData.amount) {
  cartItem.setAmount(productData.amount);
}

Parameter qty (optional)

An integer over zero. If omitted, this parameter defaults to 1.

void cartItem.setDescription(desc)

Sets the description to the desired value.

Example

if (productData.description) {
  cartItem.setDescription(productData.description);
}

Parameter desc (required)

The new description for the current item.

void cartItem.setPrice(price)

Sets the price of the current item.

Example

if (productData.price) {
  cartItem.setPrice(productData.price);
}

Parameter price (required)

The new price of the item in cents.

address cartItem.shippingAddress

Returns the shippingAddress as a JSMA address.

Example

//shipping address
var shippingAddress = cartItem.shippingAddress;

result.shippingAddress = {
  gender : shippingAddress.gender,
  firstName : shippingAddress.firstName,
  lastName : shippingAddress.lastName,
  company : shippingAddress.company,
  street : shippingAddress.street,
  houseNumber : shippingAddress.houseNumber,
  zipCode : shippingAddress.zipCode,
  city : shippingAddress.city,
  country : shippingAddress.countryCode,
  email : shippingAddress.email,
  phone : shippingAddress.phone,
  extra : shippingAddress.extra
};

return result;

int cartItem.singlePrice

Returns the price of a single item.

Example

var singlePriceText = formatMoney(cartItem.singlePrice) + " " + cart.currency;
      
if (type === "voucher") {
  singlePriceText = "";
}
              
[...]
(singlePriceEnabled) ? $("<td>").addClass("item-single-price").html(singlePriceText) : "",
(priceEnabled) ? $("<td>").addClass("item-price").html(formatMoney(cartItem.price) + " " + cart.currency) : "",
(actionsEnabled) ? $("<td>").addClass("item-actions").html(itemOptions) : ""
[...]

string cartItem.token

Returns the token if the current cartItem is a voucher.

Example

if (type === "voucher" && cartItem.token.length > 0) {
  cartItem.set('item.discounted', true);
}

string cartItem.type

Returns the type of an item.

May be one of the following:

  • product
  • voucher
  • generic

Example

//create item row
var cartRow = $("<tr>")
  .attr("data-item-type", cartItem.type)
  .attr("data-item-uuid", cartItem.uuid)
  .attr("data-item-price", cartItem.price)
  .addClass("cart-table-row");

cartRow.append(
  (previewEnabled) ? $("<td>").addClass("item-preview").append(previewImage) : "",
  (nameEnabled) ? $("<td>").addClass("item-name").html(cartItem.display) : "",
  (amountEnabled) ? $("<td>").addClass("item-amount").html(amountChooser) : "",
  (singlePriceEnabled) ? $("<td>").addClass("item-single-price").html(singlePriceText) : "",
  (priceEnabled) ? $("<td>").addClass("item-price").html(formatMoney(cartItem.price) + " " + cart.currency) : "",
  (actionsEnabled) ? $("<td>").addClass("item-actions").html(itemOptions) : ""
);

string cartItem.uuid

Returns the UUID of the current item

Example

//create item row
var cartRow = $("<tr>")
  .attr("data-item-type", cartItem.type)
  .attr("data-item-uuid", cartItem.uuid)
  .attr("data-item-price", cartItem.price)
  .addClass("cart-table-row");

cartRow.append(
  (previewEnabled) ? $("<td>").addClass("item-preview").append(previewImage) : "",
  (nameEnabled) ? $("<td>").addClass("item-name").html(cartItem.display) : "",
  (amountEnabled) ? $("<td>").addClass("item-amount").html(amountChooser) : "",
  (singlePriceEnabled) ? $("<td>").addClass("item-single-price").html(singlePriceText) : "",
  (priceEnabled) ? $("<td>").addClass("item-price").html(formatMoney(cartItem.price) + " " + cart.currency) : "",
  (actionsEnabled) ? $("<td>").addClass("item-actions").html(itemOptions) : ""
);

float cartItem.vat

Returns the VAT field if the current item is of type 'product'.

Example

if (cartItem.vat === 19.0) {
  cartItem.set('item.vat', cartItem.vat);
}

cartItem cartItem.voucher

Returns the JSMA voucher of the current item if it is of type 'voucher'.

Example

//special handling for voucher - may only exist once
else if (cartItem.type === "voucher") {
  hasVoucher = true;
        
  var voucher = cartItem.voucher;

  var description = cartItem.description;
        
  //default to voucher display
  if (!description) {
    description = voucher.display;
  }
        
  //handle attributes
  var attributes = {};
  cartItem.flexAttributes.forEach ( function ( key ) {
    attributes[key] = cartItem.get(key);
  });

  result.voucher = {
    id : cartItem.uuid,
    token : cartItem.token,
    value : cartItem.value,
    type : cartItem.type,
    display : description,
    price : Math.round(item.price * 100),
    attributes : attributes
  };
}