谷歌内容Api购物-列出只想要的产品功能



我正在使用下面的脚本(https://developers.google.com/apps-script/advanced/shopping-content)我想知道,不是所有的资源,我怎么能只列出产品价格呢?

/**
* Lists the products for a given merchant.
*/
function productList() {
var merchantId = 123456; // Replace this with your Merchant Center ID.
var pageToken;
var pageNum = 1;
var maxResults = 10;
do {
var products = ShoppingContent.Products.list(merchantId, {
pageToken: pageToken,
maxResults: maxResults
});
Logger.log('Page ' + pageNum);
if (products.resources) {
for (var i = 0; i < products.resources.length; i++) {
Logger.log('Item [' + i + '] ==> ' + products.resources[i]);
}
} else {
Logger.log('No more products in account ' + merchantId);
}
pageToken = products.nextPageToken;
pageNum++;
} while (pageToken);
}

在您的情况下,以下修改如何?

发件人:

Logger.log('Item [' + i + '] ==> ' + products.resources[i]);

收件人:

Logger.log('Item [' + i + '] (product price) ==> ' + products.resources[i].price.value); // or products.resources[i].price.currency

注:

  • 如果您想以数组的形式检索数据,下面的修改如何?

    function productList() {
    var merchantId = 123456; // Replace this with your Merchant Center ID.
    var pageToken;
    var pageNum = 1;
    var maxResults = 10;
    var res = []; // Added
    do {
    var products = ShoppingContent.Products.list(merchantId, {
    pageToken: pageToken,
    maxResults: maxResults
    });
    Logger.log('Page ' + pageNum);
    if (products.resources) {
    for (var i = 0; i < products.resources.length; i++) {
    res.push({id: products.resources[i].id, title: products.resources[i].title, price: products.resources[i].price.value}); // Added
    Logger.log('Item [' + i + '] (product price) ==> ' + products.resources[i].price.value); // or products.resources[i].price.currency
    }
    } else {
    Logger.log('No more products in account ' + merchantId);
    }
    pageToken = products.nextPageToken;
    pageNum++;
    } while (pageToken);
    Logger.log(res); // Added
    }
    

注意:

  • 在这个答案中,它假设您已经能够使用ShoppingContent.Products.list()方法。请小心

参考文献:

  • 方法:products.list
  • 价格

相关内容

  • 没有找到相关文章

最新更新