Skip to content

Instantly share code, notes, and snippets.

@BruceClark
Last active July 18, 2016 17:35
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 BruceClark/446a479d9cb9c8b4c5b95bf08d39c1a9 to your computer and use it in GitHub Desktop.
Save BruceClark/446a479d9cb9c8b4c5b95bf08d39c1a9 to your computer and use it in GitHub Desktop.
// *** USER CONFIGURATION ***
//Specify your Printfection API key
var pf_api_key = "PRINTFECTION_API_KEY";
//Specify non-Printfection product SKUs and if they have sizes
var shopify_only_products = [
{ productid: 12345, has_sizes: 1 },
{ productid: 98765, has_sizes: 0 } //Make sure no comma on last line
];
// *** END CONFIGURATION ***
fetch('https://'+pf_api_key+':@api.printfection.com/v2/items/')
.then(function(res) {
return res.text();
})
.then(function(body){
var obj = JSON.parse(body);
validateItems(obj.data);
})
.catch(callback);
function validateItems(items){
var pf_items = [];
// Add all PF items to simple array
for(var x=0; x < items.length; x++){
var item = {productid: items[x].id, has_sizes: items[x].sizes.length > 0 ? 1 : 0}
pf_items.push(item);
}
// Setup full list of possible products to check sizes
var sizes_check = pf_items.slice();
if(shopify_only_products.length){
for(var x=0; x < shopify_only_products.length; x++){
sizes_check.push(shopify_only_products[x]);
}
}
var skus = input.skus.split(',');
var sizes = input.sizes.split(',');
var quantities = input.quantities.split(',');
// Backfill sizes
var has_sizes = true;
for(var x=0; x < skus.length; x++){
for(var y=0; y < sizes_check.length; y++){
if(sizes_check[y].productid == skus[x]){
has_sizes = sizes_check[y].has_sizes;
break;
}
}
if(has_sizes == 1){
continue;
}
sizes.splice(x,0,0);
}
console.log('pf_items',pf_items);
var final_skus = [];
var final_sizes = [];
var final_quantities = [];
// Remove products which don't exist in PF
var found = false;
for(var x=0; x < skus.length; x++){
found = false;
for(var y=0; y < pf_items.length; y++){
if(pf_items[y].productid == skus[x]){
found = true;
break;
}
}
if(!found){
continue;
}
// Save products to final array which will be saved in the PF order
final_skus.push(skus[x]);
final_sizes.push(sizes[x]);
final_quantities.push(quantities[x]);
}
callback(null, [{
skus: final_skus,
sizes: final_sizes,
quantities: final_quantities
}]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment