尝试使用表格作为数据库来更新 WooCommerce 中的价格
Trying to use Sheets as db to update prices in WooCommerce
所以我尝试使用单个 sheet 作为价格数据库,通过 Woo API 使用 fetch 更新 WooCommerce 中的价格。
它有效,我的问题是它显然取决于数据集的大小?我不确定,因为我无法理解错误。
更新代码
function getDataloopwoo() {
const ck = 'ck_fd0992917fbbb0464d4146ad5861f51adcb36369';
const cs = 'cs_6f8061efce415355fb6cac520bd1506ad126578a';
const website = 'https://www.atopems-desarrollo.com.ar';
const optionsGet =
{
'method': 'GET',
'contentType': 'application/x-www-form-urlencoded;charset=UTF-8',
'muteHttpExceptions': true,
};
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PreciosBULK');
const codigos = sheet.getRange('A2:A').getValues();
const precios = sheet.getRange('B2:B').getValues();
const data = codigos.map(function(codigos, indice) {
return {
sku: codigos[0],
price: precios[indice][0]
}
})
const container = [];
var surl = website + '/wp-json/wc/v3/products?consumer_key=' + ck + '&consumer_secret=' + cs + '&per_page=100' + '&orderby=id' + '&order=asc' + '&status=publish' + '&page=1';
var url = surl
Logger.log(url)
var result = UrlFetchApp.fetch(url, optionsGet);
var headers = result.getAllHeaders();
var total_pages = 45;
var pages_count = 0;
while (pages_count < total_pages) {
if (result.getResponseCode() == 200) {
var wooProducts = JSON.parse(result.getContentText());
//Logger.log(result.getContentText());
}
for (var i = 0; i < wooProducts.length; i++) {
//Logger.log(i);
container.push({
sku: wooProducts[i]['sku'],
id: wooProducts[i]['id'],
price: wooProducts[i]['price']
});
}
pages_count++;
if (pages_count < total_pages) {
var surl = website + '/wp-json/wc/v3/products?consumer_key=' + ck + '&consumer_secret=' + cs + '&per_page=100' + '&orderby=id' + '&order=asc' + '&status=publish' + '&page=' + (pages_count + 1);
var url = surl
var result = UrlFetchApp.fetch(url, optionsGet);
Logger.log(url);
}
}
var data_obj = {}
for (let obj of data)
data_obj[obj.sku] = {'price': obj.price};
console.log(data_obj);
var container_obj = {}
for (let obj of container)
container_obj[obj.sku] = {'price': obj.price, 'id': obj.id};
console.log(container_obj);
const output = [];
for (let sku in container_obj) {
let data_subObj = data_obj[sku];
let container_subObj = container_obj[sku];
if (data_subObj.price > container_subObj.price) {
output.push({'id':container_subObj.id, 'regular_price':data_subObj.price});
}
}
console.log(output);
var temporary, chunk = 100;
for (let i = 0;i < output.length; i += chunk) {
temporary = output.slice(i, i + chunk);
var payloadUp = {
update: temporary
}
var headPost =
{
'method' : 'POST',
'contentType': 'application/json',
'payload': JSON.stringify(payloadUp)
};
console.log(payloadUp);
var urlPost = website + '/wp-json/wc/v3/products/batch?consumer_key=' + ck + '&consumer_secret=' + cs;
var result = UrlFetchApp.fetch(urlPost, headPost);
console.log(urlPost);
if (result.getResponseCode() == 200) {
console.log(result.getContentText());
};
}
}
我使用 var headers 获取所有 headers 但由于我正在测试 ATM,所以我没有使用它。因此,如果我从 WooCoommerce 获得大约 15-20 种产品,一切都很顺利,我会获取所有产品数据,创建一个包含 sku、id 和价格的新数组。然后将该数组与我的 sheets 中的数组与更新后的价格值进行比较,然后将 sku、id 和更新后的价格推送到一个新数组,并将该数组 POST 推送到 woocommerce 批量更新。工作正常,如果我尝试处理更多,我会收到此错误:
TypeError: Cannot read property 'price' of undefined
我真的很伤脑筋,主要是因为我是 JS 的新手。
我会 post 日志,
Logger.log(url);
https://atopems.com/wp-json/wc/v3/products?consumer_key=ck_xxx&consumer_secret=cs_xxx&per_page=100&orderby=id&order=asc&status=publish&page=51
console.log(data_obj);
Logging output too large. Truncating output. { '200': { price: 299.98 },
'201': { price: 156.9 },
'202': { price: 112.05 },
'203': { price: 100.58 },
'204': { price: 126.33 },
'205': { price: 126.53 },
'206': { price: 2858.42 },
'207': { price: 2336.79 },
'208': { price: 401.25 },
'209': { price: 378.32 },
'210': { price: 282.78 },
'211': { price: 252.21 },
'212': { price: 292.34 },
'213': { price: 309.53 },
'214': { price: 385.96 },
'215': { price: 554.1 },
console.log(container_obj);
Logging output too large. Truncating output. { '60026': { price: '2319.6', id: 24942 },
'60032': { price: '4050.7', id: 24943 },
'60033': { price: '4050.7', id: 24944 },
'60119': { price: '7195.72', id: 24945 },
BR9010: { price: '984.5', id: 24067 },
BR9013: { price: '1744.32', id: 24068 },
BR9014: { price: '1869.03', id: 24069 },
BR9015: { price: '1869.03', id: 24070 },
BR9016: { price: '984.5', id: 24071 },
BR9017: { price: '747.66', id: 24072 },
BR9026: { price: '664.52', id: 24073 },
BR9037: { price: '830.62', id: 24074 },
BR9042: { price: '830.62', id: 24075 },
BR9043: { price: '747.66', id: 24076 },
BR9048: { price: '1204.44', id: 24077 },
BR9049: { price: '955.23', id: 24078 },
BR9050: { price: '955.23', id: 24079 },
BR9052: { price: '1079.9', id: 24080 },
BR9055: { price: '955.23', id: 24081 },
BR9056: { price: '1266.63', id: 24082 },
BR9059: { price: '955.23', id: 24083 },
BR9067: { price: '830.62', id: 24084 },
BR9068: { price: '1349.13', id: 24085 }
准确的错误
17:07:38 Error
TypeError: Cannot read property 'price' of undefined
getDataloopwoo @ JSONsheet.gs:63
有问题的 sheet,以防有人想查看我正在处理的数据类型。
const data = codigos.map etc
使用 2 个变量,SKU 和 PRICE,它们分别是 sheet 中的 A 列和 B 列,就像简单的 objects.
编辑:
好的,测试 sheet 可以使用 CK 和 CS 密钥复制到测试 WooCommerce 站点。
https://docs.google.com/spreadsheets/d/14afFyj1geaNCWt_e4DE9S41wlgepWAtasK2z5_u1hdc/edit?usp=sharing
如果运行原样可以重现而不用做任何其他事情。
试过 20 页有效,45 页无效。
我不确定我还能做些什么来使其可重现。
- 我真的不明白问题的根源是什么。
修改点:
- 在您的情况下,
container
的 sku
的值似乎不存在于 data_obj
的 sku
的值中。我想你的问题可能是因为这个。
- 作为检查这个的脚本,您可以使用
const res = container.filter(e => !data_obj[e.sku])
作为您的脚本。在这种情况下,返回 [ { sku: 'L4943-0ULT', id: 3195, price: '5083.33' } ]
。当从示例电子表格中搜索此值时,会找到 l4943-0ult
。在这种情况下,字符大小写不同。这样,您的问题就出现了。我导致你出现问题的原因就是这个。
这个issue去掉后,下面的修改怎么样?
发件人:
data_obj[obj.sku] = {'price': obj.price};
收件人:
data_obj[isNaN(obj.sku) ? obj.sku.toUpperCase() : obj.sku] = {'price': obj.price};
而且,
发件人:
container_obj[obj.sku] = {'price': obj.price, 'id': obj.id};
收件人:
container_obj[isNaN(obj.sku) ? obj.sku.toUpperCase() : obj.sku] = { 'price': obj.price, 'id': obj.id };
还有,为了避免if语句中没有key,添加如下修改如何?
发件人:
if (data_subObj.price > container_subObj.price) {
收件人:
if (data_subObj && data_subObj.price > container_subObj.price) {
所以我尝试使用单个 sheet 作为价格数据库,通过 Woo API 使用 fetch 更新 WooCommerce 中的价格。 它有效,我的问题是它显然取决于数据集的大小?我不确定,因为我无法理解错误。
更新代码
function getDataloopwoo() {
const ck = 'ck_fd0992917fbbb0464d4146ad5861f51adcb36369';
const cs = 'cs_6f8061efce415355fb6cac520bd1506ad126578a';
const website = 'https://www.atopems-desarrollo.com.ar';
const optionsGet =
{
'method': 'GET',
'contentType': 'application/x-www-form-urlencoded;charset=UTF-8',
'muteHttpExceptions': true,
};
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PreciosBULK');
const codigos = sheet.getRange('A2:A').getValues();
const precios = sheet.getRange('B2:B').getValues();
const data = codigos.map(function(codigos, indice) {
return {
sku: codigos[0],
price: precios[indice][0]
}
})
const container = [];
var surl = website + '/wp-json/wc/v3/products?consumer_key=' + ck + '&consumer_secret=' + cs + '&per_page=100' + '&orderby=id' + '&order=asc' + '&status=publish' + '&page=1';
var url = surl
Logger.log(url)
var result = UrlFetchApp.fetch(url, optionsGet);
var headers = result.getAllHeaders();
var total_pages = 45;
var pages_count = 0;
while (pages_count < total_pages) {
if (result.getResponseCode() == 200) {
var wooProducts = JSON.parse(result.getContentText());
//Logger.log(result.getContentText());
}
for (var i = 0; i < wooProducts.length; i++) {
//Logger.log(i);
container.push({
sku: wooProducts[i]['sku'],
id: wooProducts[i]['id'],
price: wooProducts[i]['price']
});
}
pages_count++;
if (pages_count < total_pages) {
var surl = website + '/wp-json/wc/v3/products?consumer_key=' + ck + '&consumer_secret=' + cs + '&per_page=100' + '&orderby=id' + '&order=asc' + '&status=publish' + '&page=' + (pages_count + 1);
var url = surl
var result = UrlFetchApp.fetch(url, optionsGet);
Logger.log(url);
}
}
var data_obj = {}
for (let obj of data)
data_obj[obj.sku] = {'price': obj.price};
console.log(data_obj);
var container_obj = {}
for (let obj of container)
container_obj[obj.sku] = {'price': obj.price, 'id': obj.id};
console.log(container_obj);
const output = [];
for (let sku in container_obj) {
let data_subObj = data_obj[sku];
let container_subObj = container_obj[sku];
if (data_subObj.price > container_subObj.price) {
output.push({'id':container_subObj.id, 'regular_price':data_subObj.price});
}
}
console.log(output);
var temporary, chunk = 100;
for (let i = 0;i < output.length; i += chunk) {
temporary = output.slice(i, i + chunk);
var payloadUp = {
update: temporary
}
var headPost =
{
'method' : 'POST',
'contentType': 'application/json',
'payload': JSON.stringify(payloadUp)
};
console.log(payloadUp);
var urlPost = website + '/wp-json/wc/v3/products/batch?consumer_key=' + ck + '&consumer_secret=' + cs;
var result = UrlFetchApp.fetch(urlPost, headPost);
console.log(urlPost);
if (result.getResponseCode() == 200) {
console.log(result.getContentText());
};
}
}
我使用 var headers 获取所有 headers 但由于我正在测试 ATM,所以我没有使用它。因此,如果我从 WooCoommerce 获得大约 15-20 种产品,一切都很顺利,我会获取所有产品数据,创建一个包含 sku、id 和价格的新数组。然后将该数组与我的 sheets 中的数组与更新后的价格值进行比较,然后将 sku、id 和更新后的价格推送到一个新数组,并将该数组 POST 推送到 woocommerce 批量更新。工作正常,如果我尝试处理更多,我会收到此错误:
TypeError: Cannot read property 'price' of undefined
我真的很伤脑筋,主要是因为我是 JS 的新手。
我会 post 日志,
Logger.log(url);
https://atopems.com/wp-json/wc/v3/products?consumer_key=ck_xxx&consumer_secret=cs_xxx&per_page=100&orderby=id&order=asc&status=publish&page=51
console.log(data_obj);
Logging output too large. Truncating output. { '200': { price: 299.98 },
'201': { price: 156.9 },
'202': { price: 112.05 },
'203': { price: 100.58 },
'204': { price: 126.33 },
'205': { price: 126.53 },
'206': { price: 2858.42 },
'207': { price: 2336.79 },
'208': { price: 401.25 },
'209': { price: 378.32 },
'210': { price: 282.78 },
'211': { price: 252.21 },
'212': { price: 292.34 },
'213': { price: 309.53 },
'214': { price: 385.96 },
'215': { price: 554.1 },
console.log(container_obj);
Logging output too large. Truncating output. { '60026': { price: '2319.6', id: 24942 },
'60032': { price: '4050.7', id: 24943 },
'60033': { price: '4050.7', id: 24944 },
'60119': { price: '7195.72', id: 24945 },
BR9010: { price: '984.5', id: 24067 },
BR9013: { price: '1744.32', id: 24068 },
BR9014: { price: '1869.03', id: 24069 },
BR9015: { price: '1869.03', id: 24070 },
BR9016: { price: '984.5', id: 24071 },
BR9017: { price: '747.66', id: 24072 },
BR9026: { price: '664.52', id: 24073 },
BR9037: { price: '830.62', id: 24074 },
BR9042: { price: '830.62', id: 24075 },
BR9043: { price: '747.66', id: 24076 },
BR9048: { price: '1204.44', id: 24077 },
BR9049: { price: '955.23', id: 24078 },
BR9050: { price: '955.23', id: 24079 },
BR9052: { price: '1079.9', id: 24080 },
BR9055: { price: '955.23', id: 24081 },
BR9056: { price: '1266.63', id: 24082 },
BR9059: { price: '955.23', id: 24083 },
BR9067: { price: '830.62', id: 24084 },
BR9068: { price: '1349.13', id: 24085 }
准确的错误
17:07:38 Error
TypeError: Cannot read property 'price' of undefined
getDataloopwoo @ JSONsheet.gs:63
有问题的 sheet,以防有人想查看我正在处理的数据类型。
const data = codigos.map etc
使用 2 个变量,SKU 和 PRICE,它们分别是 sheet 中的 A 列和 B 列,就像简单的 objects.
编辑:
好的,测试 sheet 可以使用 CK 和 CS 密钥复制到测试 WooCommerce 站点。
https://docs.google.com/spreadsheets/d/14afFyj1geaNCWt_e4DE9S41wlgepWAtasK2z5_u1hdc/edit?usp=sharing
如果运行原样可以重现而不用做任何其他事情。
试过 20 页有效,45 页无效。
我不确定我还能做些什么来使其可重现。
- 我真的不明白问题的根源是什么。
修改点:
- 在您的情况下,
container
的sku
的值似乎不存在于data_obj
的sku
的值中。我想你的问题可能是因为这个。 - 作为检查这个的脚本,您可以使用
const res = container.filter(e => !data_obj[e.sku])
作为您的脚本。在这种情况下,返回[ { sku: 'L4943-0ULT', id: 3195, price: '5083.33' } ]
。当从示例电子表格中搜索此值时,会找到l4943-0ult
。在这种情况下,字符大小写不同。这样,您的问题就出现了。我导致你出现问题的原因就是这个。
这个issue去掉后,下面的修改怎么样?
发件人:
data_obj[obj.sku] = {'price': obj.price};
收件人:
data_obj[isNaN(obj.sku) ? obj.sku.toUpperCase() : obj.sku] = {'price': obj.price};
而且,
发件人:
container_obj[obj.sku] = {'price': obj.price, 'id': obj.id};
收件人:
container_obj[isNaN(obj.sku) ? obj.sku.toUpperCase() : obj.sku] = { 'price': obj.price, 'id': obj.id };
还有,为了避免if语句中没有key,添加如下修改如何?
发件人:
if (data_subObj.price > container_subObj.price) {
收件人:
if (data_subObj && data_subObj.price > container_subObj.price) {