Shopping Content Service

The Shopping Content Service allows you to use the Google Content API for Shopping in Apps Script. This API gives Google Merchant Center users the ability to upload and manage their product listings and manage their Merchant Center accounts.

For detailed information on this service, see the reference documentation for the Google Content API for Shopping. Like all advanced services in Apps Script, the Shopping Content Service utilizes the same objects, methods, and parameters as the public API.

Sample code

We now show how to use a few features of the Shopping Content Service.

Open code in new window

Insert product

This example demonstrates how to insert a single product into a given merchant center account.

function productInsert() {
  var merchantId = 123456; // Replace this with your Merchant Center ID.
  // Create a product resource and insert it
  var 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'
    }
  };

  response = ShoppingContent.Products.insert(productResource, merchantId);
  Logger.log(response); // RESTful insert returns the JSON object as a response.
}

List products

This example demonstrates how to list your products for a given merchant center account.

function productList() {
  var merchantId = 123456; // Replace this with your Merchant Center ID.
  var pageToken;
  var pageNum = 1;
  var maxResults = 10;

  // List all the products for a given merchant.
  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);
}

Batch insert products

This example uses Products.custombatch to insert three products at the same time.

function custombatch(productResource1, productResource2, productResource3) {
  var 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
      }
    ]
  };
  var response = ShoppingContent.Products.custombatch(custombatchResource);
  Logger.log(response);
}

Update account-level taxes

This sample code uses Accounttax to update the account-level tax information for a Merchant Center account. Please see our API guide for more information about account-level tax and shipping.

// Use Accounttax to update account-level taxes.
function updateAccountTax() {
  // Replace this with your Merchant Center ID.
  var merchantId = 123456;

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

  var accounttax = ShoppingContent.Accounttax.get(merchantId, accountId);
  Logger.log(accounttax);

  var 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'
      }
    ]
  };

  Logger.log(ShoppingContent.Accounttax.update(taxInfo, merchantId, accountId));
}

Feedback geben zu...