Skip to content

Instantly share code, notes, and snippets.

@BruceClark BruceClark/non-pf-products-zapier.js Secret
Last active Jul 18, 2016

Embed
What would you like to do?
// *** 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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.