Sử dụng phương thức UpdateCssProductInput để cập nhật một sản phẩm CSS hiện có bằng cách chỉ định cssProductInput.name
sản phẩm và phần nội dung JSON chứa dữ liệu bạn muốn cập nhật cho sản phẩm.
Lưu ý: Phương thức này chỉ cập nhật các thuộc tính được cung cấp trong yêu cầu cập nhật. Phản hồi chứa các thuộc tính giống như yêu cầu và không phản ánh trạng thái đầy đủ của CssProductInput
sau khi áp dụng bản cập nhật.
Bạn không nên dựa vào phản hồi để xác định trạng thái cuối cùng của CssProductInput
.
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}
Để thêm hoặc sửa đổi một thuộc tính trong sản phẩm, hãy chỉ định trường bằng giá trị mới trong phần nội dung JSON. Ví dụ được hiển thị sẽ cập nhật tiêu đề và đường liên kết ưu đãi trên dòng tiêu đề của tên sản phẩm hiện có 123/cssProductInputs/de~DE~B019G4
bằng giá trị thuộc tính được cung cấp trong nội dung yêu cầu, không thay đổi tất cả các trường khác.
HTTP
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}
{
"attributes": {
"title": "new item title",
"headlineOfferLink": "headline-offer.com"
}
}
cURL
curl --location --request PATCH 'https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API_TOKEN>' \
--data '{"attributes":{"numberOfOffers":"99","headlineOfferPrice":{"currency_code":"EUR","amount_micros":"1200000"}}}'
Java
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shopping.css.samples.v1.cssproducts;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.protobuf.FieldMask;
import com.google.shopping.css.v1.CssProductInput;
import com.google.shopping.css.v1.CssProductInputsServiceClient;
import com.google.shopping.css.v1.CssProductInputsServiceSettings;
import com.google.shopping.css.v1.UpdateCssProductInputRequest;
import shopping.css.samples.utils.Authenticator;
import shopping.css.samples.utils.Config;
/** This class demonstrates how to update a CSS Product for a given Account */
public class UpdateCssProductInput {
private static String getName(String domainId, String productId) {
return String.format("accounts/%s/cssProductInputs/%s", domainId, productId);
}
public static void updateCssProductInput(Config config, String productId) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
String name = getName(config.getDomainId().toString(), productId);
CssProductInputsServiceSettings cssProductInputsServiceSettings =
CssProductInputsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
try (CssProductInputsServiceClient cssProductInputsServiceClient =
CssProductInputsServiceClient.create(cssProductInputsServiceSettings)) {
// Updates the title of the CSS Product leaving the rest of the fields unchanged
UpdateCssProductInputRequest request =
UpdateCssProductInputRequest.newBuilder()
.setCssProductInput(
CssProductInput.newBuilder()
.setName(name)
.setAttributes(
com.google.shopping.css.v1.Attributes.newBuilder()
.setTitle("Attribute Title")
.setHeadlineOfferLink("abc.com")
.setHeadlineOfferCondition("New")
.setDescription("CSS Product description 0")
.build())
.build())
.setUpdateMask(FieldMask.newBuilder().addPaths("title").build())
.build();
System.out.println("Updating CSS Product");
CssProductInput response = cssProductInputsServiceClient.updateCssProductInput(request);
System.out.print("CSS product updated:");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
final Config config = Config.load();
// The ID uniquely identifying each product. In
// the format languageCode~countryCode~rawProvidedId
final String productId = "de~DE~rawProvidedId17";
updateCssProductInput(config, productId);
}
}
Chỉ có thể cập nhật các trường cấp cao nhất bằng yêu cầu cssProductInputs.update
.
Nếu muốn cập nhật các trường lồng nhau, bạn phải cung cấp toàn bộ đối tượng cấp cao nhất.
Ví dụ được hiển thị sẽ cập nhật đối tượng headlineOfferPrice
cấp cao nhất, bao gồm cả các trường lồng nhau của một sản phẩm hiện có, bằng dữ liệu sản phẩm được cung cấp trong phần nội dung yêu cầu, để tất cả các trường khác không bị ảnh hưởng.
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}
{
"attributes": {
"headlineOfferPrice": {
"amountMicros": "17.99",
"currencyCode": "USD"
}
}
}
Để chọn một số trường nhất định để cập nhật mà không cần thay đổi các trường khác có trong nội dung yêu cầu, bạn có thể chỉ định updateMask
. Tham số chuỗi truy vấn này phải là danh sách các trường bạn cần sửa đổi, được phân tách bằng dấu phẩy.
updateMask
rất hữu ích khi bạn muốn xác nhận rằng chỉ các trường được đặt tên mới được cập nhật.
Việc không chỉ định updateMask
tương đương với việc đánh dấu tất cả các trường trong yêu cầu cần cập nhật, như trong ví dụ. Tuy nhiên, nếu không cung cấp rõ ràng updateMask
, bạn sẽ không thể xoá các thuộc tính hiện có.
Ví dụ được hiển thị sẽ chỉ cập nhật title
của mặt hàng hiện có bằng dữ liệu sản phẩm tương ứng được cung cấp trong phần nội dung yêu cầu, để tất cả các trường khác (bao gồm cả headline offer link
) không bị ảnh hưởng.
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}?updateMask=title
{
"attributes": {
"title":"item-title",
"headlineOfferLink":"headline-offer-newer.com"
}
}
Nếu một trường được cung cấp trong danh sách updateMask nhưng không có trong nội dung của yêu cầu, thì trường đó sẽ bị xoá khỏi tài nguyên Sản phẩm (nếu có).
Ví dụ được hiển thị sẽ sử dụng updateMask để xoá giá trị cho trường title
.
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}?updateMask=title
ACCOUNT_ID:
Mã nhận dạng duy nhất cho tài khoản, ví dụ: 123
.
CSS_PRODUCT_ID:
Mã sản phẩm CSS, ví dụ: de~DE~B019G4
.
Để xoá trường title
, hãy loại bỏ trường này khỏi nội dung yêu cầu. Bạn cũng có thể gửi yêu cầu không có nội dung hoặc nội dung trống. Các trường không có trong updateMask sẽ không thay đổi.