購物內容服務

Shopping Content Service 可讓您在 Apps Script 中使用 Google Content API for Shopping。這個 API 可讓 Google Merchant Center 使用者上傳及管理產品資訊,並管理其 Merchant Center 帳戶。

如要進一步瞭解這項服務,請參閱 Google Content API for Shopping 的參考說明文件。如同 Apps Script 的所有進階服務,購物內容服務也會使用與公用 API 相同的物件、方法和參數。

參考資料

如要進一步瞭解這項服務,請參閱 Google Content API for Shopping API 的參考說明文件。如同 Apps Script 的所有進階服務,進階試算表服務會使用與公用 API 相同的物件、方法和參數。詳情請參閱「如何判定方法簽章」一文。

如要回報問題及尋求其他支援,請參閱 Google Content API for Shopping 支援指南

程式碼範例

現在,我們來示範如何使用購物內容服務中的幾項功能。

插入產品

這個範例說明如何在特定 Merchant Center 帳戶中插入單一產品。

advanced/shoppingContent.gs
/**
 * Inserts a product into the products list. Logs the API response.
 */
function productInsert() {
  const merchantId = 123456; // Replace this with your Merchant Center ID.
  // Create a product resource and insert it
  const productResource = {
    'offerId': 'book123',
    'title': 'A Tale of Two Cities',
    'description': 'A classic novel about the French Revolution',
    'link': 'http://my-book-shop.com/tale-of-two-cities.html',
    'imageLink': 'http://my-book-shop.com/tale-of-two-cities.jpg',
    'contentLanguage': 'en',
    'targetCountry': 'US',
    'channel': 'online',
    'availability': 'in stock',
    'condition': 'new',
    'googleProductCategory': 'Media > Books',
    'productType': 'Media > Books',
    'gtin': '9780007350896',
    'price': {
      'value': '2.50',
      'currency': 'USD'
    },
    'shipping': [{
      'country': 'US',
      'service': 'Standard shipping',
      'price': {
        'value': '0.99',
        'currency': 'USD'
      }
    }],
    'shippingWeight': {
      'value': '2',
      'unit': 'pounds'
    }
  };

  try {
    response = ShoppingContent.Products.insert(productResource, merchantId);
    // RESTful insert returns the JSON object as a response.
    console.log(response);
  } catch (e) {
    // TODO (Developer) - Handle exceptions
    console.log('Failed with error: $s', e.error);
  }
}

列出產品

以下範例說明如何列出特定 Merchant Center 帳戶中的產品。

advanced/shoppingContent.gs
/**
 * Lists the products for a given merchant.
 */
function productList() {
  const merchantId = 123456; // Replace this with your Merchant Center ID.
  let pageToken;
  let pageNum = 1;
  const maxResults = 10;
  try {
    do {
      const products = ShoppingContent.Products.list(merchantId, {
        pageToken: pageToken,
        maxResults: maxResults
      });
      console.log('Page ' + pageNum);
      if (products.resources) {
        for (let i = 0; i < products.resources.length; i++) {
          console.log('Item [' + i + '] ==> ' + products.resources[i]);
        }
      } else {
        console.log('No more products in account ' + merchantId);
      }
      pageToken = products.nextPageToken;
      pageNum++;
    } while (pageToken);
  } catch (e) {
    // TODO (Developer) - Handle exceptions
    console.log('Failed with error: $s', e.error);
  }
}

批次插入產品

本範例使用 Products.custombatch 同時插入三項產品。

advanced/shoppingContent.gs
/**
 * Batch updates products. Logs the response.
 * @param  {object} productResource1 The first product resource.
 * @param  {object} productResource2 The second product resource.
 * @param  {object} productResource3 The third product resource.
 */
function custombatch(productResource1, productResource2, productResource3) {
  const merchantId = 123456; // Replace this with your Merchant Center ID.
  custombatchResource = {
    'entries': [
      {
        'batchId': 1,
        'merchantId': merchantId,
        'method': 'insert',
        'productId': 'book124',
        'product': productResource1
      },
      {
        'batchId': 2,
        'merchantId': merchantId,
        'method': 'insert',
        'productId': 'book125',
        'product': productResource2
      },
      {
        'batchId': 3,
        'merchantId': merchantId,
        'method': 'insert',
        'productId': 'book126',
        'product': productResource3
      }
    ]
  };
  try {
    const response = ShoppingContent.Products.custombatch(custombatchResource);
    console.log(response);
  } catch (e) {
    // TODO (Developer) - Handle exceptions
    console.log('Failed with error: $s', e.error);
  }
}

更新帳戶層級稅金

這個程式碼範例使用 Accounttax 更新 Merchant Center 帳戶的帳戶層級稅務資訊。如要進一步瞭解帳戶層級的稅金和運送資訊,請參閱 API 指南

advanced/shoppingContent.gs
/**
 * Updates content account tax information.
 * Logs the API response.
 */
function updateAccountTax() {
  // Replace this with your Merchant Center ID.
  const merchantId = 123456;

  // Replace this with the account that you are updating taxes for.
  const accountId = 123456;

  try {
    const accounttax = ShoppingContent.Accounttax.get(merchantId, accountId);
    console.log(accounttax);

    const taxInfo = {
      accountId: accountId,
      rules: [
        {
          'useGlobalRate': true,
          'locationId': 21135,
          'shippingTaxed': true,
          'country': 'US'
        },
        {
          'ratePercent': 3,
          'locationId': 21136,
          'country': 'US'
        },
        {
          'ratePercent': 2,
          'locationId': 21160,
          'shippingTaxed': true,
          'country': 'US'
        }
      ]
    };

    console.log(ShoppingContent.Accounttax
        .update(taxInfo, merchantId, accountId));
  } catch (e) {
    // TODO (Developer) - Handle exceptions
    console.log('Failed with error: $s', e.error);
  }
}