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. |
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); } |
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); }); [...] }); |
The JSMA cartItem provides the following methods:
Returns the quantity of an item.
else if (type === "voucher") { amountChooser.append( $("<span>") .addClass("item-amount") .text(cartItem.amount) ); } |
Returns the description field of a cartItem.
else if (type === "item") { cartItemLabel.append( $("<span>") .addClass("item-description") .text(cartItem.description) ); } |
Accesses the flex attributes of a JSMA cartItem
//add cart item attributes cartItem.flexAttributes.forEach ( function ( key ) { result.attributes[key] = cartItem.get(key); }); |
Returns the value of key as string or null if no value is set under the key.
//add cart item attributes cartItem.flexAttributes.forEach ( function ( key ) { result.attributes[key] = cartItem.get(key); }); |
The key to be looked up in the flexAttributes of the cartItem.
Returns a boolean value depending on whether key is present for a given cartItem
if (cartItem.has('product.type')) { result.type = cartItem.get('product.type'); } |
The key to be checked in the flexAttributes of the cartItem.
Returns a list of JSMA file for further access to the previewImages of an item.
if (cartItem.previewImages && cartItem.previewImages.length > 0) { previewImageId = cartItem.previewImages[0].uuid; } else { previewImageId = product.productImage; } |
Returns the price of an item in cents.
//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) : "" ); |
Returns a JSMA product for the current item.
//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" ) ); |
Returns a JSMA file for the saved project of the current item.
Returns null if no project is associated with the item.
var result = { projectId : cartItem.project.uuid, projectLocation : cartItem.project.path, status : "OK" }; |
Returns the uuid of the file that is associated as project for the current item.
//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" ) ); |
Sets a key, value pair as a flex attribute to the current item.
if (request.parameter('savedproject')) { cartItem.set('ibe2.savedproject', request.parameter('savedproject')); } |
Removes the key from the flexAttributes of the cartItem
cartItem.remove('ibe2.savedproject'); |
The key to be removed from the flexAttributes of the cartItem.
Sets the quantity if the current item to the desired value.
if (productData.amount) { cartItem.setAmount(productData.amount); } |
An integer over zero. If omitted, this parameter defaults to 1.
Sets the description to the desired value.
if (productData.description) { cartItem.setDescription(productData.description); } |
The new description for the current item.
Sets the price of the current item.
if (productData.price) { cartItem.setPrice(productData.price); } |
The new price of the item in cents.
Returns the shippingAddress as a JSMA address.
//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; |
Returns the price of a single item.
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) : "" [...] |
Returns the token if the current cartItem is a voucher.
if (type === "voucher" && cartItem.token.length > 0) { cartItem.set('item.discounted', true); } |
Returns the type of an item.
May be one of the following:
//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) : "" ); |
Returns the UUID of the current item
//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) : "" ); |
Returns the VAT field if the current item is of type 'product'.
if (cartItem.vat === 19.0) { cartItem.set('item.vat', cartItem.vat); } |
Returns the JSMA voucher of the current item if it is of type 'voucher'.
//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 }; } |