עדכון מוצר בשירות CSS

כדי לעדכן מוצר CSS יחיד קיים, מציינים את המזהה cssProductInput.name של המוצר וגווף JSON שמכיל את הנתונים שרוצים לעדכן במוצר באמצעות השיטה UpdateCssProductInput.

הערה: השיטה הזו מעדכנת רק את המאפיינים שצוינו בבקשת העדכון. התשובה מכילה את אותם מאפיינים כמו הבקשה, והיא לא משקפת את המצב המלא של 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 אבל לא בגוף הבקשה, השדה הזה יימחק ממשאב Product, אם הוא קיים.

בדוגמה המוצגת נעשה שימוש ב-updateMask כדי להסיר את הערך של השדה title.

PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}?updateMask=title

ACCOUNT_ID: מזהה ייחודי של החשבון, לדוגמה 123.

CSS_PRODUCT_ID: מזהה המוצר ב-CSS, לדוגמה, de~DE~B019G4.

כדי למחוק את השדה title, משאירים אותו מחוץ לגוף הבקשה. אפשר גם לשלוח את הבקשה ללא גוף או עם גוף ריק. שדות שלא נכללים ב-updateMask לא ישתנו.