本指南假設您已逐步完成 《入門指南》 發出授權要求
完成所有設定後,您就可以傳送要求至 Google Content API for Shopping。以下程式碼 範例:示範如何傳送幾個簡單的要求:
- 建立產品。
- 取得產品清單。
- 從清單中擷取特定產品。
- 更新產品價格。
如需完整的方法清單,請參閱 參考說明文件。
詳情請參閱最佳做法頁面。 ,瞭解如何使用這個 API。 本頁面也會說明建議使用批次要求的原因。
如果您在使用 Content API 時遇到問題,請參閱 查看服務中斷的 Merchant Center 狀態資訊主頁,以及 支援頁面。
建立產品
如要查看產品的所有屬性完整詳細資料,請前往 產品動態饋給規格: 如何建立產品 ,請使用以下程式碼:
通訊協定
POST /content/v2.1/YOUR_MERCHANT_ID/products { "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": "GB", "feedLabel": "GB", "channel": "online", "availability": "in stock", "condition": "new", "googleProductCategory": "Media > Books", "gtin": "9780007350896", "price": { "value": "2.50", "currency": "GBP" }, "shipping": [{ "country": "GB", "service": "Standard shipping", "price": { "value": "0.99", "currency": "GBP" } }], "shippingWeight": { "value": "200", "unit": "grams" } }
Java
Product product = new Product(); product.setOfferId("book123"); product.setTitle("A Tale of Two Cities"); product.setDescription("A classic novel about the French Revolution"); product.setLink("http://my-book-shop.com/tale-of-two-cities.html"); product.setImageLink("http://my-book-shop.com/tale-of-two-cities.jpg"); product.setContentLanguage("en"); product.setTargetCountry("GB"); product.setChannel("online"); product.setAvailability("in stock"); product.setCondition("new"); product.setGoogleProductCategory("Media > Books"); product.setGtin("9780007350896"); Price price = new Price(); price.setValue("2.50"); price.setCurrency("GBP"); product.setPrice(price); Price shippingPrice = new Price(); shippingPrice.setValue("0.99"); shippingPrice.setCurrency("GBP"); ProductShipping shipping = new ProductShipping(); shipping.setPrice(shippingPrice); shipping.setCountry("GB"); shipping.setService("Standard shipping"); ArrayListshippingList = new ArrayList (); shippingList.add(shipping); product.setShipping(shippingList); Product result = service.products().insert(merchantId, product).execute();
PHP
$product = new Google_Service_ShoppingContent_Product(); $product->setOfferId('book123'); $product->setTitle('A Tale of Two Cities'); $product->setDescription('A classic novel about the French Revolution'); $product->setLink('http://my-book-shop.com/tale-of-two-cities.html'); $product->setImageLink('http://my-book-shop.com/tale-of-two-cities.jpg'); $product->setContentLanguage('en'); $product->setTargetCountry('GB'); $product->setChannel('online'); $product->setAvailability('in stock'); $product->setCondition('new'); $product->setGoogleProductCategory('Media > Books'); $product->setGtin('9780007350896'); $price = new Google_Service_ShoppingContent_Price(); $price->setValue('2.50'); $price->setCurrency('GBP'); $shipping_price = new Google_Service_ShoppingContent_Price(); $shipping_price->setValue('0.99'); $shipping_price->setCurrency('GBP'); $shipping = new Google_Service_ShoppingContent_ProductShipping(); $shipping->setPrice($shipping_price); $shipping->setCountry('GB'); $shipping->setService('Standard shipping'); $shipping_weight = new Google_Service_ShoppingContent_ProductShippingWeight(); $shipping_weight->setValue(200); $shipping_weight->setUnit('grams'); $product->setPrice($price); $product->setShipping(array($shipping)); $product->setShippingWeight($shipping_weight); $result = $service->products->insert($merchant_id, $product);
取得產品清單
若要取得 產品,請使用以下程式碼:
通訊協定
GET /content/v2.1/YOUR_MERCHANT_ID/products
Java
List productsList = service.products().list(merchantId); ProductsListResponse page = productsList.execute(); while ((page.getResources() != null) && !page.getResources().isEmpty()) { for (Product product : page.getResources()) { System.out.printf("%s %s%n", product.getId(), product.getTitle()); } if (page.getNextPageToken() == null) { break; } productsList.setPageToken(page.getNextPageToken()); page = productsList.execute(); }
PHP
$products = $service->products->listProducts($merchantId); $parameters = array(); while (!empty($products->getResources()) { foreach ($products->getResources() as $product) { printf("%s %s\n", $product->getId(), $product->getTitle()); } if (!empty($products->getNextPageToken()) { break; } $parameters['pageToken'] = $products->nextPageToken; $products = $service->products->listProducts($merchantId, $parameters); }
擷取特定產品
要擷取 從清單中找出產品,請使用以下程式碼:
通訊協定
GET /content/v2.1/YOUR_MERCHANT_ID/products/online:en:GB:book123
Java
Product product = service.products() .get(merchantId, "online:en:GB:book123") .execute(); System.out.printf("%s %s\n", product.getId(), product.getTitle());
PHP
$product = $service->products->get($merchant_id, 'online:en:GB:book123'); printf("%s %s\n", $product->getId(), $product->getTitle());
更新線上產品供應情形
您可以使用 產品資源中的補充動態饋給,包含以下內容更新線上產品資料 程式碼:
通訊協定
POST /content/v2.1/YOUR_MERCHANT_ID/products?YOUR_SUPPLEMENTAL_FEED_ID { "offerId": "book123", "contentLanguage": "en", "targetCountry": "GB", "feedLabel": "GB", "channel": "online", "availability": "out of stock" }
Java
Product product = new Product(); // Mandatory Fields product.setOfferId("book123"); product.setContentLanguage("en"); product.setTargetCountry("GB"); product.setChannel("online"); // Optional Fields to Update product.setAvailability("out of stock"); // Your unique supplemental feedId feedId=123456789 Product result = service.products().insert(merchantId, product, feedId).execute();
PHP
$product = new Google_Service_ShoppingContent_Product(); // Mandatory Fields $product->setOfferId('book123'); $product->setContentLanguage('en'); $product->setTargetCountry('GB'); $product->setChannel('online'); // Optional Fields to Update $product->setAvailability('out of stock'); // Your unique supplemental feedId $feedId=123456789 $result = $service->products->insert($merchant_id, $product, $feedId);
更新店面產品供應情形
您可以使用 店面商品目錄資料服務:使用以下程式碼更新店面產品資料:
通訊協定
POST /content/v2.1/YOUR_MERCHANT_ID/localinventory/online/products/online:en:GB:book123 { "availability": "out of stock" }
Java
Product product = new Product(); // Mandatory Fields product.setOfferId("book123"); product.setContentLanguage("en"); product.setTargetCountry("GB"); product.setChannel("online"); // Optional Fields to Update product.setAvailability("out of stock"); Product result = service.localinventory().insert(merchantId, product).execute();
PHP
$product = new Google_Service_ShoppingContent_Product(); // Mandatory Fields $product->setOfferId('book123'); $product->setContentLanguage('en'); $product->setTargetCountry('GB'); $product->setChannel('online'); // Optional Fields to Update $product->setAvailability('out of stock'); $result = $service->localinventory->insert($merchant_id, $product);