插入 CSS 商品

使用此方法可插入商品或更新现有商品。

如果商品已存在,则此方法将用作 upsert 方法。这个 方法会更新所有给定数据,但删除未提供的数据 。因此,您必须重新发送所有字段值。例如,如果您只 添加 headlineOfferPricerawProvidedId 属性,然后添加所有其他属性 值。

cURL

curl --location 'https://css.googleapis.com/v1/accounts/1234567/cssProductInputs:insert' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API_TOKEN>' \
--data '{"rawProvidedId":"test_css_product_input","feedLabel":"FR","contentLanguage":"fr","attributes":{"title":"item-title-1","headlineOfferLink":"headline-offer.com","customLabel0":"cla0","headlineOfferCondition":"new","description":"CSSProductDescription","numberOfOffers":"123","cppLink":"cpp_link.com","brand":"test brand","googleProductCategory":"Media>Books","gtin":"3614030018942","imageLink":"http://example.com/0.jpg","headlineOfferPrice":{"currency_code":"EUR","amount_micros":"2000000"},"certifications":[{"code":"12","authority":"EuropeanCommission","name":"EPREL"}],"productHighlights":["test highlight"],"productDetails":[{"sectionName":"test section","attributeName":"test attribute","attributeValue":"test value"}]}}'

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.shopping.css.v1.CssProductInput;
import com.google.shopping.css.v1.CssProductInputsServiceClient;
import com.google.shopping.css.v1.CssProductInputsServiceSettings;
import com.google.shopping.css.v1.InsertCssProductInputRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import shopping.css.samples.utils.Authenticator;
import shopping.css.samples.utils.Config;

/** This class demonstrates how to insert a CSS Product for a given Account */
public class InsertCssProductInput {

  private static String getParent(String domainId) {
    return String.format("accounts/%s", domainId);
  }

  private static String getName(String domainId, String productId) {
    return String.format("accounts/%s/cssProductInputs/%s", domainId, productId);
  }

  public static void insertCssProductInput(Config config, long feedId, String rawProvidedId)
      throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    CssProductInputsServiceSettings cssProductInputsServiceSettings =
        CssProductInputsServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String parent = getParent(config.getDomainId().toString());

    try (CssProductInputsServiceClient cssProductInputsServiceClient =
        CssProductInputsServiceClient.create(cssProductInputsServiceSettings)) {

      InsertCssProductInputRequest request =
          InsertCssProductInputRequest.newBuilder()
              .setParent(parent)
              .setCssProductInput(
                  CssProductInput.newBuilder()
                      .setRawProvidedId(rawProvidedId)
                      .setFeedLabel("DE")
                      .setContentLanguage("de")
                      .setAttributes(
                          com.google.shopping.css.v1.Attributes.newBuilder()
                              .setTitle("Attribute Title")
                              .setHeadlineOfferLink("abc.com")
                              .setHeadlineOfferCondition("New")
                              .setDescription("CSS Product description 0")
                              .setNumberOfOffers(123)
                              .setCppLink("cpp_link.com")
                              .setBrand("CSS brand")
                              .setGoogleProductCategory("Media > Books")
                              .setGtin("3614030018941")
                              .setHeadlineOfferPrice(
                                  com.google.shopping.type.Price.newBuilder().build())
                              .build())
                      .build())
              .setFeedId(feedId)
              .build();

      System.out.println("Sending InsertCssProductInput request");
      CssProductInput response = cssProductInputsServiceClient.insertCssProductInput(request);
      System.out.println("Inserted CssProduct Name below");
      System.out.println(response.getName());
    } 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 feed
    final long feedId = 0;

    // Create a thread pool to insert multiple CSS Products in parallel
    ExecutorService threadPool = Executors.newCachedThreadPool();
    for (int i = 0; i < 100; i++) {
      // The raw ID identifying each product
      final String rawProvidedId = "rawProvidedId" + i;
      threadPool.execute(
          () -> {
            try {
              insertCssProductInput(config, feedId, rawProvidedId);
            } catch (Exception e) {
              System.out.println(e);
            }
          });
    }
  }
}

JSON

{
  "rawProvidedId": "test_css_product_input",
  "feedLabel": "FR",
  "contentLanguage": "fr",
  "attributes": {
    "title": "item-title-1",
    "headlineOfferLink": "headline-offer.com",
    "customLabel0": "cla0",
    "headlineOfferCondition": "new",
    "description": "CSS Product Description",
    "numberOfOffers": "123",
    "cppLink": "cpp_link.com",
    "brand": "test brand",
    "googleProductCategory": "Media > Books",
    "gtin": "3614030018942",
    "imageLink": "http://example.com/0.jpg",
    "headlineOfferPrice": {
      "currency_code": "EUR",
      "amount_micros": "2000000"
    },
    "certifications": [{
      "code": "12",
      "authority": "European Commission",
      "name": "EPREL"
    }],
    "productHighlights": [
      "test highlight"
    ],
    "productDetails": [{
      "sectionName": "test section",
      "attributeName": "test attribute",
      "attributeValue": "test value"
    }]
  }
}

其他属性

如需查看完整的属性列表,请参阅 CSS 商品数据 规范

Google Merchant Center Feed 中的大多数与商品相关的属性 富媒体商品商家 API 在这里也适用