使用 UpdateCssProductInput 方法更新現有的單一 CSS 產品,方法是指定產品 cssProductInput.name
和 JSON 主體,其中包含要為產品更新的資料。
注意:這個方法只會更新更新要求中提供的屬性。回應包含與要求相同的屬性,且不會反映套用更新後的完整 CssProductInput
狀態。您不應依據回應來判斷 CssProductInput
的最終狀態。
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}
如要新增或修改產品中的屬性,請在 JSON 主體中指定具有新值的欄位。顯示的範例會使用要求主體中提供的屬性值,更新現有產品名稱 123/cssProductInputs/de~DE~B019G4
的標題和主打商品連結,並保留所有其他欄位。
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);
}
}
只有頂層欄位可以透過 cssProductInputs.update
要求進行更新。如果您想更新巢狀欄位,則必須提供整個頂層物件。
這個範例會使用要求主體提供的產品資料,更新最高層級的 headlineOfferPrice
物件 (包括現有產品的巢狀欄位),不變動所有其他欄位。
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}
{
"attributes": {
"headlineOfferPrice": {
"amountMicros": "17.99",
"currencyCode": "USD"
}
}
}
如要選取要更新的特定欄位,而不變更要求內文中包含的其他欄位,您可以指定 updateMask
。這個查詢字串參數應為以半形逗號分隔的清單,其中包含您需要修改的欄位。如果您想斷言只更新指定的欄位,updateMask
就很實用。如未指定 updateMask
,就等同於標記要求中的所有欄位皆要更新,如範例所示。不過,如果未明確提供 updateMask
,就無法刪除現有屬性。
在這個範例中,系統會只使用要求主體中提供的產品資料,更新現有項目的 title
,其他欄位 (包括 headline offer link
) 則保持不變。
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}?updateMask=title
{
"attributes": {
"title":"item-title",
"headlineOfferLink":"headline-offer-newer.com"
}
}
如果在 updateMask 清單中提供欄位,但未在要求的內容中提供,則系統會從產品資源中刪除該欄位 (如果有)。
這個範例會使用 updateMask 移除 title
欄位的值。
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}?updateMask=title
ACCOUNT_ID:帳戶專屬 ID,例如 123
。
CSS_PRODUCT_ID:CSS 產品 ID,例如 de~DE~B019G4
。
如要刪除 title
欄位,請將其從要求主體中移除。您也可以傳送沒有主體或空白主體的要求。不在 updateMask 中的欄位將保持不變。