Created
December 30, 2015 00:43
-
-
Save SeanLF/adc59f27a3c0bd1790aa to your computer and use it in GitHub Desktop.
Shopify developer intern application task solution [Winter 2016]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For this code to run, it is assumed that jQuery is available.