更新 CSS 產品

使用 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 中未列出的欄位將維持不變。