หน้านี้อธิบายวิธีอัปโหลดและจัดการผลิตภัณฑ์แบบเป็นโปรแกรม เมื่อใช้ Merchant Products API คุณจะแทรกหรืออัปเดตผลิตภัณฑ์ในแหล่งข้อมูล เรียกข้อมูลผลิตภัณฑ์จากบัญชี และลบผลิตภัณฑ์ออกจากแหล่งข้อมูลได้
Merchant Products API มีทรัพยากร 2 รายการ
productInputs
represent the input parts of your products.products
represent the processed products that was constructed from your input parts.
productInputs
อาจเป็นข้อมูลหลักและข้อมูลเสริม ทั้งนี้ขึ้นอยู่กับว่ามีการอัปโหลดไปยังแหล่งข้อมูลหลักหรือแหล่งข้อมูลเสริม
product
แต่ละรายการจะสร้างขึ้นจาก productInput
หลักรายการเดียวและ productInputs
เสริมจำนวนเท่าใดก็ได้
คุณสามารถใช้ Merchant Products API เพื่อสร้างแคตตาล็อกร้านค้าออนไลน์หรือร้านค้าในพื้นที่ ซึ่งคือผลิตภัณฑ์ที่ปรากฏในปลายทางการช็อปปิ้งหลายแห่ง
คุณสามารถใช้แหล่งข้อมูล productInputs
เมื่อสร้างบัญชี Merchant Center, ตั้งค่าแหล่งข้อมูลแรก และพร้อมที่จะอัปโหลดชุดผลิตภัณฑ์แรกผ่าน API แล้ว
แม้ว่าผู้ขายจะอัปโหลดผลิตภัณฑ์โดยใช้ไฟล์ได้ ซึ่งเรียกว่า PrimaryProductDataSource แต่การสร้างและลบผลิตภัณฑ์โดยใช้ Merchant API มีข้อดีหลายประการ ข้อดีเหล่านี้รวมถึงเวลาในการตอบสนองที่เร็วขึ้นและความสามารถในการอัปเดตผลิตภัณฑ์แบบเรียลไทม์โดยไม่ต้องจัดการไฟล์ขนาดใหญ่ ระบบอาจใช้เวลาถึงหลายชั่วโมงเพื่อให้การเปลี่ยนแปลงผลิตภัณฑ์จากการเรียก API แสดงในฐานข้อมูล Shopping
ข้อกำหนดเบื้องต้น
หากไม่มีแหล่งข้อมูล ให้สร้างแหล่งข้อมูลโดยใช้ Merchant Data Sources API หรือ Merchant Center
หากมีแหล่งข้อมูลที่สร้างขึ้นโดยใช้ UI ของ Merchant Center หรือ API อยู่แล้ว คุณจะใช้ Merchant Products API เพื่อเพิ่มผลิตภัณฑ์ได้ หากคุณใช้ Content API for Shopping เพื่อเพิ่มผลิตภัณฑ์ โปรดดูคู่มือการย้ายข้อมูลเพื่อทำความเข้าใจวิธีเริ่มต้นใช้งาน Merchant Products API
คุณมีหน้าที่รับผิดชอบในการปฏิบัติตามนโยบายโฆษณา Shopping และข้อมูลที่แสดงฟรี โฆษณา Shopping สงวนสิทธิ์ในการบังคับใช้นโยบายเหล่านี้และดำเนินการตามความเหมาะสมหากพบเนื้อหาหรือพฤติกรรมที่ละเมิดนโยบายเหล่านี้
แหล่งข้อมูล
ทรัพยากร products
ช่วยให้คุณเรียกข้อมูลผลิตภัณฑ์จากฐานข้อมูล Shopping ได้
แหล่งข้อมูล productInput
แสดงข้อมูลอินพุตที่คุณส่งสำหรับผลิตภัณฑ์ นอกจากนี้ ยังมีวิธีการต่างๆ ที่ช่วยให้คุณอัปเดตหรือลบข้อมูลผลิตภัณฑ์ทีละรายการหรือทีละหลายรายการในโหมดกลุ่ม แหล่งข้อมูล
productInput
ต้องมีฟิลด์ต่อไปนี้
channel
: ช่องทางของผลิตภัณฑ์offerId
: ตัวระบุที่ไม่ซ้ำกันสำหรับผลิตภัณฑ์contentLanguage
: รหัสภาษา 2 ตัวอักษรตามมาตรฐาน ISO 639-1 ของผลิตภัณฑ์feedLabel
: ป้ายกำกับที่ช่วยให้คุณจัดหมวดหมู่และระบุผลิตภัณฑ์ได้ อักขระสูงสุดที่อนุญาตคือ 20 ตัว และอักขระที่รองรับคือA-Z
,0-9
, ขีดกลางสั้น และขีดล่าง ป้ายกํากับฟีดต้องไม่มีการเว้นวรรค ดูข้อมูลเพิ่มเติมได้ที่การใช้ป้ายกำกับฟีด
อัปโหลดอินพุตผลิตภัณฑ์ลงในบัญชี
หากต้องการอัปโหลดข้อมูลผลิตภัณฑ์ลงในบัญชี ให้ใช้วิธีการ accounts.productInputs.insert
คุณต้องส่งตัวระบุที่ไม่ซ้ำกันของแหล่งข้อมูลหลักหรือแหล่งข้อมูลเสริม
คําขอตัวอย่างต่อไปนี้แสดงวิธีใช้เมธอด accounts.productInputs.insert
เพื่ออัปโหลดอินพุตผลิตภัณฑ์ไปยังบัญชีผู้ขาย คำขอจะกำหนดราคาและภูมิภาคการจัดส่ง รวมถึงแอตทริบิวต์ที่กำหนดเอง เช่น วันที่ผลิตและขนาด
POST https://merchantapi.googleapis.com/products/v1beta/accounts/{ACCOUNT_ID}/productInputs:insert?dataSource={DATASOURCE}
{
"name": "{PRODUCT_TITLE}",
"versionNumber": {VERSION_NUMBER},
"contentLanguage": "{CONTENT_LANGUAGE}",
"feedLabel": "{FEED_LABEL}",
"offerId": "{OFFER_ID}",
"channel": "ONLINE",
"attributes": {
"availability": "in stock",
"imageLink": "{IMAGE_LINK}",
"link": "{PRODUCT_LINK}",
"brand": "{BRAND_NAME}",
"price": {
"currencyCode": "{CURRENCY_CODE}",
"amountMicros": {PRICE}
},
"color": "red",
"productWeight": {
"value": 320,
"unit": "g"
},
"adult": false,
"shipping": [
{
"country": "GB",
"price": {
"amountMicros": {SHIPPING_COST},
"currencyCode": "{CURRENCY_CODE_SHIPPING}"
},
"postalCode": "{SHIPPING_POSTALCODE}",
"service": "",
"region": "{SHIPPING_REGION}",
"maxHandlingTime": "{MAX_HANDLING_TIME}",
"minHandlingTime": "{MIN_HANDLING_TIME}",
"maxTransitTime": "{MAX_TRANSIT_TIME}",
"minTransitTime": "{MIN_TRANSIT_TIME}"
}
],
"gender": "Female"
},
"customAttributes": [
{
"name": "size",
"value": "Large"
},
{
"name": "Date of Manufacturing",
"value": "2024-05-05"
}
]
}
แทนที่ค่าต่อไปนี้
- {ACCOUNT_ID}: ตัวระบุที่ไม่ซ้ำของบัญชี Merchant Center
- {DATASOURCE}: ตัวระบุที่ไม่ซ้ำกันของแหล่งข้อมูล โดยควรอยู่ในรูปแบบ
accounts/
{ACCOUNT_ID}/dataSources/
{DATASOURCE_ID} - {PRODUCT_TITLE}: ชื่อผลิตภัณฑ์
- {VERSION_NUMBER}: หมายเลขเวอร์ชันของผลิตภัณฑ์ ไม่บังคับ
- {CONTENT_LANGUAGE}: รหัสภาษา ISO 639-1 แบบ 2 อักขระของผลิตภัณฑ์ ต้องระบุ
- {FEED_LABEL}: ป้ายกำกับที่ช่วยให้คุณจัดหมวดหมู่และระบุผลิตภัณฑ์ อนุญาตให้ป้อนอักขระได้สูงสุด 20 ตัว และอักขระที่รองรับคือ
A-Z
,0-9
, ขีดกลางสั้น และขีดล่าง ป้ายกํากับฟีดต้องไม่มีการเว้นวรรค - {OFFER_ID}: ตัวระบุที่ไม่ซ้ำกันของผลิตภัณฑ์ ต้องระบุ
- {IMAGE_LINK}: ลิงก์ไปยังรูปภาพของผลิตภัณฑ์ในเว็บไซต์ ไม่บังคับ
- {PRODUCT_LINK}: ลิงก์ไปยังผลิตภัณฑ์ในเว็บไซต์ ไม่บังคับ
- {CURRENCY_CODE}: สกุลเงินของราคาโดยใช้คำย่อ 3 ตัวตาม ISO 4217 ไม่บังคับ
- {PRICE}: ราคาของผลิตภัณฑ์ที่แสดงเป็นตัวเลขในหน่วยไมโคร ไม่บังคับ
- {SHIPPING_COST}: ราคาจัดส่งแบบคงที่ที่แสดงเป็นตัวเลข ไม่บังคับ
- {SHIPPING_POSTALCODE}: ช่วงรหัสไปรษณีย์ที่มีอัตราค่าจัดส่ง ไม่บังคับ
- {MAX_HANDLING_TIME}: วันทำการสูงสุดก่อนจัดส่งนับตั้งแต่ได้รับคำสั่งซื้อจนถึงเวลาที่จัดส่ง ไม่บังคับ
- {MIN_HANDLING_TIME}: วันทำการขั้นต่ำก่อนจัดส่งนับตั้งแต่เวลาที่ลูกค้าสั่งซื้อสินค้าจนถึงเวลาที่จัดส่งสินค้า ค่า 0 หมายความว่าคำสั่งซื้อได้รับการนำส่งในวันเดียวกันกับที่ได้รับคำสั่งซื้อ ไม่บังคับ
- {MAX_TRANSIT_TIME}: เวลาขนส่งสูงสุดเป็นจำนวนวันทำการนับจากวันที่จัดส่งคำสั่งซื้อจนถึงวันที่นำส่ง ไม่บังคับ
- {MIN_TRANSIT_TIME}: เวลาขนส่งขั้นต่ำเป็นจำนวนวันทำการตั้งแต่เวลาที่จัดส่งคำสั่งซื้อจนถึงเวลาที่นำส่ง ค่า 0 หมายความว่าคำสั่งซื้อได้รับการนำส่งในวันเดียวกันกับที่จัดส่ง ไม่บังคับ
เมื่อคำขอทำงานสำเร็จแล้ว คุณจะเห็นการตอบกลับต่อไปนี้
{
"name": "{PRODUCT_NAME}",
"product": "{PRODUCT_ID}",
"channel": "ONLINE",
"offerId": "{OFFER_ID}",
"contentLanguage": "{CONTENT_LANGUAGE}",
"feedLabel": "{FEED_LABEL}",
"versionNumber": "{VERSION_NUMBER}",
"attributes": {
"link": "{PRODUCT_LINK}",
"imageLink": "{IMAGE_LINK}",
"adult": false,
"availability": "in stock",
"brand": "{BRAND_NAME}",
"color": "red",
"gender": "Female",
"price": {
"amountMicros": "{PRICE}",
"currencyCode": "{CURRENCY_CODE}"
},
"shipping": [
{
"price": {
"amountMicros": "{SHIPPING_COST}",
"currencyCode": "{CURRENCY_CODE}"
},
"country": "{SHIPPING_COUNTRY}",
"region": "{SHIPPING_REGION}",
"postalCode": "{SHIPPING_POSTALCODE}",
"minHandlingTime": "{MIN_HANDLING_TIME}",
"maxHandlingTime": "{MAX_HANDLING_TIME}",
"minTransitTime": "{MIN_TRANSIT_TIME}",
"maxTransitTime": "{MAX_TRANSIT_TIME}"
}
],
"productWeight": {
"value": 320,
"unit": "g"
}
},
"customAttributes": [
{
"name": "Size",
"value": "Large"
},
{
"name": "Date of Manufacturing",
"value": "2024-05-05"
}
]
}
หากต้องการเพิ่มผลิตภัณฑ์ลงในบัญชี Merchant Center คุณสามารถใช้วิธี google.shopping.merchant.accounts.v1beta.InsertProductInputSample
ดังที่แสดงในตัวอย่างต่อไปนี้
Java
public static void insertProductInput(Config config, String dataSource) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
ProductInputsServiceSettings productInputsServiceSettings =
ProductInputsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates parent to identify where to insert the product.
String parent = getParent(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (ProductInputsServiceClient productInputsServiceClient =
ProductInputsServiceClient.create(productInputsServiceSettings)) {
// Price to be used for shipping ($33.45).
Price price = Price.newBuilder().setAmountMicros(33_450_000).setCurrencyCode("USD").build();
Shipping shipping =
Shipping.newBuilder()
.setPrice(price)
.setCountry("GB")
.setService("1st class post")
.build();
Shipping shipping2 =
Shipping.newBuilder()
.setPrice(price)
.setCountry("FR")
.setService("1st class post")
.build();
Attributes attributes =
Attributes.newBuilder()
.setTitle("A Tale of Two Cities")
.setDescription("A classic novel about the French Revolution")
.setLink("https://exampleWebsite.com/tale-of-two-cities.html")
.setImageLink("https://exampleWebsite.com/tale-of-two-cities.jpg")
.setAvailability("in stock")
.setCondition("new")
.setGoogleProductCategory("Media > Books")
.setGtin(0, "9780007350896")
.addShipping(shipping)
.addShipping(shipping2)
.build();
// The datasource can be either a primary or supplemental datasource.
InsertProductInputRequest request =
InsertProductInputRequest.newBuilder()
.setParent(parent)
// You can only insert products into datasource types of Input "API" and "FILE", and
// of Type "Primary" or "Supplemental."
// This field takes the `name` field of the datasource.
.setDataSource(dataSource)
// If this product is already owned by another datasource, when re-inserting, the
// new datasource will take ownership of the product.
.setProductInput(
ProductInput.newBuilder()
.setChannel(ChannelEnum.ONLINE)
.setContentLanguage("en")
.setFeedLabel("label")
.setOfferId("sku123")
.setAttributes(attributes)
.build())
.build();
System.out.println("Sending insert ProductInput request");
ProductInput response = productInputsServiceClient.insertProductInput(request);
System.out.println("Inserted ProductInput Name below");
// The last part of the product name will be the product ID assigned to a product by Google.
// Product ID has the format `channel~contentLanguage~feedLabel~offerId`
System.out.println(response.getName());
System.out.println("Inserted Product Name below");
System.out.println(response.getProduct());
} catch (Exception e) {
System.out.println(e);
}
}
เรียกข้อมูลผลิตภัณฑ์ที่ประมวลผลแล้วจากบัญชี
หากต้องการเรียกข้อมูลผลิตภัณฑ์ที่ประมวลผลแล้วจากบัญชี ให้ใช้วิธีการ accounts.products.get
ระบบอาจใช้เวลาหลายนาทีเพื่อให้ผลิตภัณฑ์ที่ประมวลผลแล้วปรากฏขึ้นหลังจากการแทรก
GET https://merchantapi.googleapis.com/products/v1beta/accounts/{ACCOUNT_ID}/products/{PRODUCT_NAME}
แทนที่ {PRODUCT_NAME} ด้วยชื่อแหล่งข้อมูลอินพุตของผลิตภัณฑ์ที่ต้องการลบ เช่น online~en~US~sku123
คุณดูชื่อทรัพยากรของผลิตภัณฑ์ที่ประมวลผลแล้วได้จากช่อง product
ในการตอบกลับของ accounts.productInputs.insert
หากต้องการเรียกข้อมูลผลิตภัณฑ์รายการเดียวสำหรับบัญชี Merchant Center หนึ่งๆ ให้ใช้วิธี google.shopping.merchant.accounts.v1beta.GetProductRequest
ดังที่แสดงในตัวอย่างต่อไปนี้
Java
public static void getProduct(Config config, String product) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
ProductsServiceSettings productsServiceSettings =
ProductsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Calls the API and catches and prints any network failures/errors.
try (ProductsServiceClient productsServiceClient =
ProductsServiceClient.create(productsServiceSettings)) {
// The name has the format: accounts/{account}/products/{productId}
GetProductRequest request = GetProductRequest.newBuilder().setName(product).build();
System.out.println("Sending get product request:");
Product response = productsServiceClient.getProduct(request);
System.out.println("Retrieved Product below");
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
ลบข้อมูลผลิตภัณฑ์ออกจากบัญชี
หากต้องการลบข้อมูลผลิตภัณฑ์ออกจากบัญชี ให้ใช้วิธี accounts.productInputs.delete
คุณต้องส่งตัวระบุที่ไม่ซ้ำกันของแหล่งข้อมูลหลักหรือแหล่งข้อมูลเสริมที่เป็นของผลิตภัณฑ์เพื่อลบผลิตภัณฑ์โดยใช้ Merchant Products API
คำขอต่อไปนี้แสดงวิธีใช้accounts.productInputs.delete
วิธีนี้เพื่อลบอินพุตผลิตภัณฑ์
DELETE https://merchantapi.googleapis.com/products/v1beta/accounts/{ACCOUNT_ID}/productInputs/{PRODUCT_NAME}?dataSource=accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}
แทนที่ {PRODUCT_NAME} ด้วยชื่อแหล่งข้อมูลอินพุตของผลิตภัณฑ์ที่ต้องการลบ เช่น online~en~US~sku123
หากต้องการลบผลิตภัณฑ์สำหรับบัญชี Merchant Center หนึ่งๆ ให้ใช้วิธี google.shopping.merchant.accounts.v1beta.DeleteProductInputRequest
ดังที่แสดงในตัวอย่างต่อไปนี้
Java
public static void deleteProductInput(Config config, String productId, String dataSource)
throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
ProductInputsServiceSettings productInputsServiceSettings =
ProductInputsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates product name to identify product.
String name =
ProductInputName.newBuilder()
.setAccount(config.getAccountId().toString())
.setProductinput(productId)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (ProductInputsServiceClient productInputsServiceClient =
ProductInputsServiceClient.create(productInputsServiceSettings)) {
DeleteProductInputRequest request =
DeleteProductInputRequest.newBuilder().setName(name).setDataSource(dataSource).build();
System.out.println("Sending deleteProductInput request");
productInputsServiceClient.deleteProductInput(request); // no response returned on success
System.out.println(
"Delete successful, note that it may take a few minutes for the delete to update in"
+ " the system. If you make a products.get or products.list request before a few"
+ " minutes have passed, the old product data may be returned.");
} catch (Exception e) {
System.out.println(e);
}
}
แสดงผลิตภัณฑ์จากบัญชี
หากต้องการแสดงผลิตภัณฑ์ที่ประมวลผลแล้วในบัญชี ให้ใช้accounts.products.list
วิธีนี้ ตามที่แสดงในคำขอต่อไปนี้
GET https://merchantapi.googleapis.com/products/v1beta/accounts/{ACCOUNT_ID}/products
หากต้องการแสดงผลิตภัณฑ์สำหรับบัญชี Merchant Center หนึ่งๆ ให้ใช้วิธี google.shopping.merchant.accounts.v1beta.ListProductsRequest
ดังที่แสดงในตัวอย่างต่อไปนี้
Java
public static void listProducts(Config config) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
ProductsServiceSettings productsServiceSettings =
ProductsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates parent to identify the account from which to list all products.
String parent = getParent(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (ProductsServiceClient productsServiceClient =
ProductsServiceClient.create(productsServiceSettings)) {
// The parent has the format: accounts/{account}
ListProductsRequest request = ListProductsRequest.newBuilder().setParent(parent).build();
System.out.println("Sending list products request:");
ListProductsPagedResponse response = productsServiceClient.listProducts(request);
int count = 0;
// Iterates over all rows in all pages and prints the datasource in each row.
// Automatically uses the `nextPageToken` if returned to fetch all pages of data.
for (Product product : response.iterateAll()) {
System.out.println(product); // The product includes the `productStatus` field
// That shows approval and disapproval information.
count++;
}
System.out.print("The following count of products were returned: ");
System.out.println(count);
} catch (Exception e) {
System.out.println("An error has occured: ");
System.out.println(e);
}
}