Last active
May 5, 2017 07:44
-
-
Save liesislukas/1473ec0876cc264c4c7d317af211dc06 to your computer and use it in GitHub Desktop.
WooCommerce: update attributes for all products by using search to filter the products before update.
This file contains hidden or 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
var WooCommerceAPI = require('woocommerce-api'); | |
var WooCommerce = new WooCommerceAPI({ | |
url: 'https://xxxx.lt', | |
consumerKey: 'xxxxx', | |
consumerSecret: 'xxxxx', | |
wpAPI: true, | |
version: 'wc/v1' | |
}); | |
function helper({timeout, id, value, data}) { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
WooCommerce.put(`products/${id}`, data, function (err, data, res) { | |
if (res.indexOf('Cloudflare') !== -1) { | |
console.log('#klfkds Cloudflare ERROR!'); | |
resolve(false); | |
} else { | |
console.log(`atnaujino preke ${id} ir pridejo gamintoja ${value}`); | |
resolve(true); | |
} | |
}); | |
}, timeout); | |
}); | |
} | |
function doIt({search, page, value}) { | |
return new Promise((resolve) => { | |
WooCommerce.get(`products?search=${search}&per_page=100&page=${page}`, function (err, data, res) { | |
// console.log(err, res); | |
let howMany = 0; | |
res = JSON.parse(res); | |
let promises = []; | |
console.log(`res: ${res.length}`); | |
res.forEach((prodcut) => { | |
let gamintojasFound = false; | |
if (prodcut.attributes && prodcut.attributes.length) { | |
prodcut.attributes.forEach((attribute) => { | |
if (attribute.id === 51) { | |
gamintojasFound = true; | |
// console.log(prodcut.id); | |
// console.log(prodcut.attributes); | |
// console.log('-------------------'); | |
} | |
}); | |
} | |
if (gamintojasFound === false) { | |
// console.log(prodcut.id); | |
// console.log(prodcut.attributes); | |
// console.log('-------------------'); | |
let data = {attributes: prodcut.attributes}; | |
data.attributes.push({ | |
id: 51, | |
name: 'Gamintojas', | |
position: 2, | |
visible: true, | |
variation: false, | |
options: [value] | |
}); | |
// | |
// console.log(`atnaujins products/${prodcut.id}`); | |
howMany++; | |
let timeout = 100 * howMany; | |
promises.push(helper({timeout, id: prodcut.id, value, data})); | |
} | |
}); | |
Promise.all(promises).then((results) => { | |
let allGood = true; | |
results.forEach((res) => { | |
if (res === false) { | |
allGood = false; | |
} | |
}); | |
resolve({allGood, needNextPage: res.length > 80}); | |
}); | |
}); | |
}); | |
} | |
let all = []; | |
all.push({search: 'Casablanca', value: '„Casablanca“, Vokietija'}); | |
all.push({search: 'Cilio', value: '„Cilio", Vokietija'}); | |
all.push({search: 'CIS France', value: '„CIS France", Prancūzija'}); | |
all.push({search: 'gamma', value: '„Gamma", Italija'}); | |
all.push({search: 'GGS Solingen', value: '„GGS Solingen", Vokietija'}); | |
all.push({search: 'Grunwerg', value: '„Grunwerg", Anglija'}); | |
all.push({search: 'HIT', value: '„HIT“, Olandija'}); | |
all.push({search: 'Kaufgut', value: '„Kaufgut", Italija'}); | |
all.push({search: 'Kruger', value: '„Kruger", Vokietija'}); | |
all.push({search: 'Kucenprofi', value: '„Kuchenprofi", Vokietija'}); | |
all.push({search: 'Kuchenprofi', value: '„Kuchenprofi", Vokietija'}); | |
all.push({search: 'Kuchenprofi', value: '„Kuchenprofi", Vokietija'}); | |
all.push({search: 'Schwerter', value: '„Schwerter", Vokietija'}); | |
all.push({search: 'SSW', value: '„SSW", Vokietija'}); | |
all.push({search: 'WEIS', value: '„WEIS", Vokietija'}); | |
all.push({search: 'Zassenhaus', value: '„Zassenhaus", Vokietija'}); | |
// let row = all[6]; | |
// let page = 1; | |
recursiveDo({i: 0, page: 1}); | |
function recursiveDo({i, page}) { | |
let row = all[i]; | |
if (row) { | |
doIt({search: row.search, page, value: row.value}).then((result) => { | |
if (result.allGood !== true) { | |
recursiveDo({i, page}); | |
} else if (result.allGood === true && result.needNextPage === true) { | |
recursiveDo({i, page: page + 1}); | |
} else if (result.allGood === true && result.needNextPage === false) { | |
recursiveDo({i: i + 1, page: 1}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment