From f75ff72fa8e7b7680fd9a8737249fa9c6d50f55c Mon Sep 17 00:00:00 2001 From: fcsobel Date: Sun, 16 Feb 2014 00:42:26 -0600 Subject: [PATCH] Update simpleCart.js Add SendForm options to allow item formatting setting index start. Ex used for mvc model binder: type: "SendForm", itemFormat: 'items[{index}].{name}', indexStart: 0 Results in: items[0].name items[0].quantity items[0].price items[0].options items[1].name items[1].quantity items[1].price items[1].options ...etc --- simpleCart.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/simpleCart.js b/simpleCart.js index cfeeb0b..a2ee16d 100755 --- a/simpleCart.js +++ b/simpleCart.js @@ -1136,6 +1136,15 @@ return simpleCart.error('URL required for SendForm Checkout'); } + if (!opts.itemFormat) { + opts.itemFormat = 'item_{name}_{index}'; + } + + if (isUndefined(opts.indexStart)) { + opts.indexStart = 1; + } + + // build basic form options var data = { currency : simpleCart.currency().code @@ -1150,12 +1159,17 @@ // add items to data simpleCart.each(function (item,x) { - var counter = x+1, + var counter = x+opts.indexStart, options_list = [], send; - data['item_name_' + counter] = item.get('name'); - data['item_quantity_' + counter] = item.quantity(); - data['item_price_' + counter] = item.price(); + + // replace index + var itemFormat = opts.itemFormat.replace('{index}', counter); + + // set the data + data[itemFormat.replace('{name}', 'name')] = item.get('name'); + data[itemFormat.replace('{name}', 'quantity')] = item.quantity(); + data[itemFormat.replace('{name}', 'price')] = item.price(); // create array of extra options simpleCart.each(item.options(), function (val,x,attr) { @@ -1170,7 +1184,7 @@ }); // add the options to the description - data['item_options_' + counter] = options_list.join(", "); + data[itemFormat.replace('{name}', 'options')] = options_list.join(", "); });