Dịch vụ nội dung mua sắm

Dịch vụ nội dung mua sắm cho phép bạn sử dụng Google Content API for Shopping trong Apps Script. API này cho phép người dùng Google Merchant Center tải lên và quản lý trang thông tin sản phẩm cũng như quản lý tài khoản Merchant Center.

Để biết thông tin chi tiết về dịch vụ này, hãy xem tài liệu tham khảo dành cho Google Content API for Shopping. Giống như tất cả các dịch vụ nâng cao trong Apps Script, Dịch vụ nội dung mua sắm sử dụng cùng đối tượng, phương thức và tham số như API công khai.

Tài liệu tham khảo

Để biết thông tin chi tiết về dịch vụ này, hãy xem tài liệu tham khảo dành cho Google Content API for Shopping API. Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ Trang tính nâng cao sử dụng cùng đối tượng, phương thức và tham số như API công khai. Để biết thêm thông tin, hãy xem phần Cách xác định chữ ký phương thức.

Để báo cáo vấn đề và tìm nguồn hỗ trợ khác, hãy xem Hướng dẫn hỗ trợ dành cho Google Content API for Shopping.

Mã mẫu

Giờ chúng tôi sẽ trình bày cách sử dụng một số tính năng của Dịch vụ nội dung mua sắm.

Chèn sản phẩm

Ví dụ này minh hoạ cách chèn một sản phẩm vào một tài khoản nhất định cho trung tâm người bán.

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);
  }
}

Liệt kê sản phẩm

Ví dụ này minh hoạ cách đăng thông tin sản phẩm cho một tài khoản Merchant Center cụ thể.

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);
  }
}

Chèn hàng loạt sản phẩm

Ví dụ này sử dụng Products.custombatch để chèn ba sản phẩm cùng một lúc.

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);
  }
}

Cập nhật các khoản thuế ở cấp tài khoản

Mã mẫu này sử dụng Accounttax (Thuế tài khoản) để cập nhật thông tin thuế ở cấp tài khoản cho tài khoản Merchant Center. Vui lòng xem hướng dẫn về API của chúng tôi để biết thêm thông tin về thuế và thông tin vận chuyển ở cấp tài khoản.

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);
  }
}