Skip to content

Commit

Permalink
Merge pull request #14 from haubc/BIG-16042
Browse files Browse the repository at this point in the history
BIG-16042 AddToCart JS for Stencil Utils
  • Loading branch information
haubc committed May 12, 2015
2 parents 4e4f405 + 915b1b8 commit 5e4e766
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
23 changes: 23 additions & 0 deletions src/remote/cart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import _ from 'lodash';
import RemoteBC from './remote';

export default class Cart extends RemoteBC
{
/**
* Add item to cart with options (variants)
*
* @param {Number} productId
* @param {Number} qty
* @param {Object} options
* @param {Function} callback
*/
itemAdd(productId, qty, options, callback) {
let payload = {
product_id: productId,
qty: qty
};

// add the attributes
_.forEach(options, (val, key) => {
payload[key] = val;
});

this.makeRequest('/cart/add', 'POST', payload, callback);
}

/**
* Update cart item quantity
*
Expand Down
26 changes: 4 additions & 22 deletions src/remote/product-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,14 @@ export default class RemoteProductAttributes extends RemoteBC

/**
*
* @param jQuery $container
* @param productId
* @param {Object} options
* @param {Number} productId
* @param callback
*/
optionChange($container, productId, callback)
optionChange(options, productId, callback)
{
let url = this.endPoint + productId,
// What does this query mean?
//
// :input:radio:checked
// Get all radios that are checked (since they are grouped together by name).
// If the query is just :input alone, it will return all radios (even the ones that aren't selected).
//
// :input:not(:radio)
// This is to retrieve all text, hidden, dropdown fields that don't have "groups".
$optionValues = $container.find(':input:radio:checked, :input:not(:radio)'),
params = {};

// iterate over values
$optionValues.each((index, ele) => {
let $ele = $(ele),
name = $ele.attr('name'),
val = $ele.val();

params[name] = val;
});
params = options;

this.makeRequest(url, 'POST', params, callback);
}
Expand Down

0 comments on commit 5e4e766

Please sign in to comment.