更新 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:账号的唯一标识符,例如 123

CSS_PRODUCT_ID:CSS 商品 ID,例如 de~DE~B019G4

如需删除 title 字段,请将其从请求正文中移除。您也可以发送不含正文或正文为空的请求。不在 updateMask 中的字段将保持不变。