Trang này giải thích cách bạn có thể tải sản phẩm lên và quản lý sản phẩm theo phương thức lập trình. Khi sử dụng API Sản phẩm của người bán, bạn có thể chèn hoặc cập nhật sản phẩm trong nguồn dữ liệu, truy xuất sản phẩm từ tài khoản và xoá sản phẩm khỏi nguồn dữ liệu.
Merchant Products API chứa hai tài nguyên.
productInputs
đại diện cho các phần đầu vào của sản phẩm.products
đại diện cho các sản phẩm đã xử lý được tạo từ các phần đầu vào.
productInputs
có thể là chính và bổ sung, tuỳ thuộc vào việc bạn tải tệp này lên nguồn dữ liệu chính hay nguồn dữ liệu bổ sung.
Mỗi product
sẽ được tạo từ một productInput
chính và bất kỳ số lượng productInputs
bổ sung nào.
Bạn có thể sử dụng Merchant Products API để tạo danh mục cửa hàng trực tuyến hoặc tại cửa hàng địa phương. Đây là những sản phẩm có thể xuất hiện trên nhiều vị trí mua sắm.
Bạn có thể sử dụng tài nguyên productInputs
sau khi tạo tài khoản Merchant Center, thiết lập nguồn dữ liệu đầu tiên và sẵn sàng tải một nhóm sản phẩm ban đầu lên thông qua API.
Mặc dù người bán có thể tải sản phẩm lên bằng tệp có tên là PrimaryProductDataSource, nhưng việc tạo và xoá sản phẩm bằng Merchant API có một số ưu điểm. Những lợi thế này bao gồm thời gian phản hồi nhanh hơn và khả năng cập nhật sản phẩm theo thời gian thực mà không cần quản lý các tệp lớn. Có thể mất đến vài giờ để các thay đổi đối với sản phẩm do lệnh gọi API thực hiện xuất hiện trong cơ sở dữ liệu Mua sắm.
Điều kiện tiên quyết
Nếu bạn chưa có nguồn dữ liệu, hãy tạo nguồn dữ liệu bằng Merchant Data Sources API hoặc Merchant Center.
Nếu đã tạo một nguồn dữ liệu bằng giao diện người dùng Merchant Center hoặc bằng API, thì bạn có thể sử dụng Merchant Products API để thêm sản phẩm. Nếu bạn đang sử dụng Content API for Shopping để thêm sản phẩm, hãy tham khảo hướng dẫn di chuyển để tìm hiểu cách bắt đầu sử dụng Merchant Products API.
Bạn có trách nhiệm tuân thủ các chính sách về quảng cáo Mua sắm và trang thông tin miễn phí. Quảng cáo Mua sắm giữ quyền thực thi các chính sách này và phản hồi thích đáng nếu phát hiện nội dung hoặc hành vi vi phạm các chính sách này.
Tài nguyên
Tài nguyên products
cho phép bạn truy xuất thông tin sản phẩm từ cơ sở dữ liệu Mua sắm.
Tài nguyên productInput
đại diện cho dữ liệu đầu vào mà bạn gửi cho một sản phẩm. Lớp này cũng cung cấp các phương thức cho phép bạn cập nhật hoặc xoá từng thông tin sản phẩm hoặc nhiều thông tin sản phẩm cùng một lúc ở chế độ hàng loạt. Tài nguyên productInput
phải có các trường sau:
channel
: Kênh của sản phẩm.offerId
: Giá trị nhận dạng duy nhất của sản phẩm.contentLanguage
: Mã ngôn ngữ ISO 639-1 gồm hai chữ cái cho sản phẩm.feedLabel
: Nhãn giúp bạn phân loại và xác định sản phẩm. Số ký tự tối đa được phép là 20 và các ký tự được hỗ trợ làA-Z
,0-9
, dấu gạch nối và dấu gạch dưới. Nhãn nguồn cấp dữ liệu không được chứa dấu cách. Để biết thêm thông tin, hãy xem bài viết Sử dụng nhãn nguồn cấp dữ liệu.
Tải dữ liệu đầu vào về sản phẩm lên tài khoản
Để tải dữ liệu đầu vào về sản phẩm lên tài khoản, hãy sử dụng phương thức accounts.productInputs.insert
. Bạn phải truyền giá trị nhận dạng duy nhất của nguồn dữ liệu chính hoặc bổ sung.
Yêu cầu mẫu sau đây cho biết cách bạn có thể sử dụng phương thức accounts.productInputs.insert
để tải dữ liệu đầu vào về sản phẩm lên tài khoản người bán. Yêu cầu này đặt giá vận chuyển và khu vực, cũng như các thuộc tính tuỳ chỉnh như ngày sản xuất và kích thước.
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"
}
]
}
Thay thế nội dung sau:
- {ACCOUNT_ID}: Mã nhận dạng duy nhất của tài khoản Merchant Center.
- {DATASOURCE}: Giá trị nhận dạng duy nhất của nguồn dữ liệu. Tên này phải ở định dạng
accounts/
{ACCOUNT_ID}/dataSources/
{DATASOURCE_ID}. - {PRODUCT_TITLE}: Tên sản phẩm.
- {VERSION_NUMBER}: Số phiên bản của sản phẩm. Không bắt buộc.
- {CONTENT_LANGUAGE}: Mã ngôn ngữ ISO 639-1 gồm hai chữ cái cho sản phẩm. Bắt buộc.
- {FEED_LABEL}: Nhãn cho phép bạn phân loại và xác định sản phẩm. Số ký tự tối đa được phép là 20 và các ký tự được hỗ trợ là
A-Z
,0-9
, dấu gạch nối và dấu gạch dưới. Nhãn nguồn cấp dữ liệu không được chứa dấu cách. - {OFFER_ID}: Mã nhận dạng duy nhất của sản phẩm. Bắt buộc.
- {IMAGE_LINK}: Đường liên kết đến hình ảnh của sản phẩm trên trang web của bạn. Không bắt buộc.
- {PRODUCT_LINK}: Đường liên kết đến sản phẩm trên trang web của bạn. Không bắt buộc.
- {CURRENCY_CODE}: Đơn vị tiền tệ của giá bán, sử dụng từ viết tắt gồm ba chữ cái theo ISO 4217. Không bắt buộc.
- {PRICE}: Giá của sản phẩm được biểu thị dưới dạng số theo micro. Không bắt buộc.
- {SHIPPING_COST}: Giá vận chuyển cố định được biểu thị dưới dạng số. Không bắt buộc.
- {SHIPPING_POSTALCODE}: Phạm vi mã bưu chính áp dụng mức phí vận chuyển. Không bắt buộc.
- {MAX_HANDLING_TIME}: Thời gian xử lý tối đa tính theo ngày làm việc, từ khi nhận được đơn đặt hàng đến khi giao đơn đặt hàng. Không bắt buộc.
- {MIN_HANDLING_TIME}: Thời gian xử lý tối thiểu tính theo ngày làm việc, từ khi nhận được đơn đặt hàng đến khi vận chuyển đơn đặt hàng. Giá trị 0 có nghĩa là đơn đặt hàng được giao trong cùng ngày nhận đơn đặt hàng. Không bắt buộc.
- {MAX_TRANSIT_TIME}: Thời gian vận chuyển tối đa tính theo ngày làm việc, từ khi đơn đặt hàng được vận chuyển đến khi đơn đặt hàng được giao. Không bắt buộc.
- {MIN_TRANSIT_TIME}: Thời gian vận chuyển tối thiểu tính theo ngày làm việc từ khi đơn đặt hàng được vận chuyển đến khi đơn đặt hàng được giao. Giá trị 0 có nghĩa là đơn đặt hàng được giao trong cùng ngày với ngày vận chuyển. Không bắt buộc.
Khi yêu cầu chạy thành công, bạn sẽ thấy phản hồi sau:
{
"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"
}
]
}
Để thêm sản phẩm vào tài khoản Merchant Center, bạn có thể sử dụng phương thức google.shopping.merchant.accounts.v1beta.InsertProductInputSample
như trong mẫu sau.
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);
}
}
Truy xuất sản phẩm đã xử lý từ tài khoản của bạn
Để truy xuất một sản phẩm đã xử lý từ tài khoản của bạn, hãy sử dụng phương thức accounts.products.get
. Có thể mất vài phút thì sản phẩm đã xử lý mới xuất hiện sau khi chèn.
GET https://merchantapi.googleapis.com/products/v1beta/accounts/{ACCOUNT_ID}/products/{PRODUCT_NAME}
Thay thế {PRODUCT_NAME} bằng tên của tài nguyên đầu vào sản phẩm mà bạn muốn xoá. Ví dụ: online~en~US~sku123
.
Bạn có thể lấy tên tài nguyên của sản phẩm đã xử lý từ trường product
trong phản hồi của accounts.productInputs.insert
.
Để truy xuất một sản phẩm cho một tài khoản Merchant Center nhất định, bạn có thể sử dụng phương thức google.shopping.merchant.accounts.v1beta.GetProductRequest
như trong mẫu sau.
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);
}
}
Xoá thông tin sản phẩm khỏi tài khoản
Để xoá dữ liệu đầu vào về sản phẩm khỏi tài khoản, hãy sử dụng phương thức accounts.productInputs.delete
. Bạn phải truyền giá trị nhận dạng duy nhất của nguồn dữ liệu chính hoặc bổ sung mà sản phẩm thuộc về để xoá sản phẩm bằng Merchant Products API.
Yêu cầu sau đây cho biết cách bạn có thể sử dụng phương thức accounts.productInputs.delete
để xoá dữ liệu đầu vào về sản phẩm:
DELETE https://merchantapi.googleapis.com/products/v1beta/accounts/{ACCOUNT_ID}/productInputs/{PRODUCT_NAME}?dataSource=accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}
Thay thế {PRODUCT_NAME} bằng tên của tài nguyên đầu vào sản phẩm mà bạn muốn xoá. Ví dụ: online~en~US~sku123
.
Để xoá một sản phẩm cho một tài khoản Merchant Center nhất định, bạn có thể sử dụng phương thức google.shopping.merchant.accounts.v1beta.DeleteProductInputRequest
như trong mẫu sau.
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);
}
}
Liệt kê sản phẩm trong tài khoản
Để liệt kê các sản phẩm đã xử lý trong tài khoản của bạn, hãy sử dụng phương thức accounts.products.list
, như trong yêu cầu sau.
GET https://merchantapi.googleapis.com/products/v1beta/accounts/{ACCOUNT_ID}/products
Để đăng sản phẩm cho một tài khoản Merchant Center nhất định, bạn có thể sử dụng phương thức google.shopping.merchant.accounts.v1beta.ListProductsRequest
như trong mẫu sau.
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);
}
}