Aggiorna prodotto CSS

Utilizza il metodo UpdateCssProductInput per aggiornare un singolo prodotto CSS esistente specificando il prodottocssProductInput.name e un corpo JSON contenente i dati da aggiornare per il prodotto.

Nota: questo metodo aggiorna solo gli attributi forniti nella richiesta di aggiornamento. La risposta contiene gli stessi attributi della richiesta e non riflette lo stato completo di CssProductInput dopo l'applicazione dell'aggiornamento. Non devi fare affidamento sulla risposta per determinare lo stato finale del CssProductInput.

PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}

Per aggiungere o modificare un attributo in un prodotto, specifica il campo con il nuovo valore nel corpo JSON. L'esempio mostrato aggiorna il titolo e il link all'offerta principale di un nome prodotto esistente 123/cssProductInputs/de~DE~B019G4 con il valore dell'attributo fornito nel corpo della richiesta, lasciando invariati tutti gli altri campi.

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);
  }
}

Solo i campi di primo livello possono essere aggiornati con la richiesta cssProductInputs.update. Se vuoi aggiornare i campi nidificati, devi fornire l'intero oggetto di primo livello.

L'esempio mostrato aggiorna l'oggetto headlineOfferPrice di primo livello, inclusi i campi nidificati di un prodotto esistente, con i dati di prodotto forniti nel corpo della richiesta, lasciando invariati tutti gli altri campi.

PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}
{
  "attributes": {
    "headlineOfferPrice": {
      "amountMicros": "17.99",
      "currencyCode": "USD"
    }
  }
}

Per selezionare determinati campi da aggiornare senza apportare modifiche agli altri inclusi nel corpo della richiesta, puoi specificare un updateMask. Questo parametro della stringa di query deve essere un elenco di campi da modificare separati da virgole. Un updateMask è utile quando vuoi affermare che verranno aggiornati solo i campi denominati. La mancata specifica di un updateMask equivale a contrassegnare tutti i campi della richiesta da aggiornare, come mostrato nell'esempio. Tuttavia, se un updateMask non viene fornito esplicitamente, non è possibile eliminare gli attributi esistenti.

L'esempio mostrato aggiornerà solo il campo title dell'articolo esistente con i rispettivi dati di prodotto forniti nel corpo della richiesta, lasciando invariati tutti gli altri campi, incluso il campo 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"
  }
}

Se un campo viene fornito nell'elenco updateMask, ma non nel corpo della richiesta, verrà eliminato dalla risorsa Prodotto, se esistente.

L'esempio mostrato utilizzerà updateMask per rimuovere il valore del campo title.

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

ACCOUNT_ID: identificatore univoco dell'account, ad esempio 123.

CSS_PRODUCT_ID: ID prodotto CSS, ad esempio de~DE~B019G4.

Per eliminare il campo title, non includerlo nel corpo della richiesta. Puoi anche inviare la richiesta senza corpo o con un corpo vuoto. I campi non presenti in updateMask rimarranno invariati.