ใช้เมธอด 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 เช่น de~DE~B019G4
หากต้องการลบฟิลด์ title
ให้ปล่อยฟิลด์นี้ไว้นอกเนื้อหาคำขอ นอกจากนี้ คุณยังส่งคำขอโดยไม่มีเนื้อหาหรือเนื้อหาว่างเปล่าได้อีกด้วย ฟิลด์ที่ไม่ได้อยู่ใน updateMask จะยังคงเหมือนเดิม