Skip to content

Instantly share code, notes, and snippets.

@SeanLF
Created December 30, 2015 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanLF/adc59f27a3c0bd1790aa to your computer and use it in GitHub Desktop.
Save SeanLF/adc59f27a3c0bd1790aa to your computer and use it in GitHub Desktop.
Shopify developer intern application task solution [Winter 2016]
// Global variables
var PRODUCTS = {};
var CATEGORIES_SEARCHED_FOR = ['Wallet', 'Lamp'];
var TOTAL_PRICE = 0, TOTAL_WEIGHT = 0;
function get_price_for_all_variants_for_searched_categories(products){
// Filter all the products on the search
PRODUCTS = PRODUCTS.filter(function(product){
// Indicates if the product's category is found in the search list
return CATEGORIES_SEARCHED_FOR.indexOf(product.product_type) !== -1;
});
// For each product category that is searched for
$.each(PRODUCTS, function(i, product){
// For each variant of a lamp or wallet
$.each(product.variants, function(j, variant){
// If it is available, add the price to the total price and add the total weight for shipping cost.
if(variant.available){
TOTAL_PRICE += parseFloat(variant.price); TOTAL_WEIGHT += variant.grams;
}
});
});
return TOTAL_PRICE;
}
function displayResult(price){
// Log the price in the console
console.log('Cart subtotal: $' + TOTAL_PRICE);
}
// Get all products
$.getJSON('https://shopicruit.myshopify.com/products.json', function (data) {
PRODUCTS = data.products;
var sub_total = get_price_for_all_variants_for_searched_categories(PRODUCTS);
displayResult(sub_total);
});
@SeanLF
Copy link
Author

SeanLF commented Dec 30, 2015

For this code to run, it is assumed that jQuery is available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment