Aby zaktualizować pojedynczy produkt w usłudze porównywania cen, użyj metody UpdateCssProductInput
, podając nazwę produktu
cssProductInput.name i treść JSON zawierającą dane, które chcesz
zaktualizować.
Uwaga: ta metoda aktualizuje tylko atrybuty podane w żądaniu aktualizacji. Odpowiedź zawiera te same atrybuty co żądanie i nie odzwierciedla pełnego stanu CssProductInput po zastosowaniu aktualizacji.
Nie należy polegać na odpowiedzi, aby określić ostateczny stan CssProductInput.
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}
Aby dodać lub zmodyfikować atrybut w produkcie, podaj pole z nową wartością w treści JSON. Pokazany przykład zaktualizuje tytuł
i link do strony oferty w nagłówku
istniejącego produktu o nazwie 123/cssProductInputs/de~DE~B019G4 o
wartość atrybutu podaną w treści żądania, pozostawiając wszystkie inne pola
bez zmian.
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);
}
}
Za pomocą żądania cssProductInputs.update można aktualizować tylko pola najwyższego poziomu.
Jeśli chcesz zaktualizować pola zagnieżdżone, musisz podać cały obiekt najwyższego poziomu.
Pokazany przykład zaktualizuje obiekt najwyższego poziomu headlineOfferPrice, w tym pola zagnieżdżone istniejącego produktu, o dane produktu podane w treści żądania, pozostawiając wszystkie inne pola bez zmian.
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}
{
"attributes": {
"headlineOfferPrice": {
"amountMicros": "17.99",
"currencyCode": "USD"
}
}
}
Aby wybrać określone pola do zaktualizowania bez wprowadzania zmian w pozostałych polach zawartych w treści żądania, możesz określić updateMask. Ten parametr ciągu zapytania powinien być listą pól rozdzielonych przecinkami, które chcesz zmodyfikować.
updateMask jest przydatny, gdy chcesz mieć pewność, że zaktualizowane zostaną tylko nazwane pola.
Nieokreślenie updateMask jest równoznaczne z oznaczeniem wszystkich pól w żądaniu do zaktualizowania, jak pokazano w przykładzie. Jeśli jednak updateMask nie zostanie podany wyraźnie, nie będzie można usunąć istniejących atrybutów.
Pokazany przykład zaktualizuje tylko title istniejącego elementu o odpowiednie dane produktu podane w treści żądania, pozostawiając wszystkie inne pola, w tym headline offer link, bez zmian.
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}?updateMask=title
{
"attributes": {
"title":"item-title",
"headlineOfferLink":"headline-offer-newer.com"
}
}
Jeśli pole jest podane na liście updateMask, ale nie w treści żądania, zostanie usunięte z zasobu Product, jeśli istnieje.
Pokazany przykład użyje updateMask do usunięcia wartości pola title.
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}?updateMask=title
ACCOUNT_ID:
Unikalny identyfikator konta, np. 123.
CSS_PRODUCT_ID:
identyfikator produktu w usłudze porównywania cen, np. de~DE~B019G4.
Aby usunąć pole title, pomiń je w treści żądania. Możesz też wysłać żądanie bez treści lub z pustą treścią. Pola, które nie znajdują się w updateMask, pozostaną bez zmian.