با استفاده از متد 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 را با مقدار ویژگی ارائه شده در بدنه درخواست بهروزرسانی میکند و سایر فیلدها را دست نخورده باقی میگذارد.
اچتیپی
PATCH https://css.googleapis.com/v1/accounts/{ACCOUNT_ID}/cssProductInputs/{CSS_PRODUCT_ID}
{
"attributes": {
"title": "new item title",
"headlineOfferLink": "headline-offer.com"
}
}
حلقه
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"}}}'
جاوا
// 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 نباشند، بدون تغییر باقی میمانند.