จัดการการจัดกลุ่มตามภูมิภาค

ภูมิภาค Merchant API แสดงถึงภูมิภาคทางภูมิศาสตร์ที่คุณใช้เป็นเป้าหมายที่เกี่ยวข้องกับทรัพยากร accounts.products.regionalInventories ได้ คุณสามารถกำหนดภูมิภาคเป็นคอลเล็กชันของรหัสไปรษณีย์ หรือในบางประเทศ ให้ใช้การกำหนดเป้าหมายตามภูมิศาสตร์ที่กำหนดไว้ล่วงหน้า ดูข้อมูลเพิ่มเติมได้ที่ตั้งค่า ภูมิภาค

Merchant API มีปลายทางแบบกลุ่มสำหรับการจัดการภูมิภาค ซึ่งช่วยให้คุณสร้าง อัปเดต และลบภูมิภาคได้สูงสุด 100 ภูมิภาคในการเรียก API ครั้งเดียว วิธีนี้เหมาะอย่างยิ่งสำหรับผู้ขายที่จัดการความพร้อมจำหน่ายสินค้าและการกำหนดราคาระดับภูมิภาค (RAAP) ในวงกว้าง ซึ่งช่วยเพิ่มประสิทธิภาพและลดความซับซ้อนในการผสานรวม

ภาพรวม

Batch API ช่วยให้คุณทำสิ่งต่อไปนี้ได้ด้วยวิธีที่เกี่ยวข้อง

  • สร้างภูมิภาคหลายภูมิภาค ในคำขอเดียว: regions:batchCreate
  • ลบภูมิภาคหลายภูมิภาค พร้อมกัน: regions:batchDelete
  • อัปเดตภูมิภาคหลายภูมิภาค พร้อมกัน: regions:batchUpdate

ข้อกำหนดเบื้องต้น

คำขอแบบกลุ่มทั้งหมดต้องมีบทบาทผู้ใช้ผู้ดูแลระบบ สำหรับการตรวจสอบสิทธิ์

สร้างภูมิภาคหลายภูมิภาค

ตัวอย่างนี้แสดงวิธีสร้างภูมิภาคใหม่ 2 ภูมิภาค ได้แก่ ภูมิภาคที่กำหนดโดยรหัสไปรษณีย์และอีกภูมิภาคที่กำหนดโดยการกำหนดเป้าหมายตามภูมิศาสตร์ ในการเรียก BatchCreateRegions ครั้งเดียว

ส่งคำขอ

สร้าง URL คำขอได้ดังนี้

POST
https://merchantapi.googleapis.com/accounts/v1/accounts/{ACCOUNT_ID}/regions:batchCreate

เนื้อหาคำขอจะมีรายการ requests ซึ่งแต่ละออบเจ็กต์จะระบุ regionId และข้อมูล region ที่จะสร้าง

{
  "requests": [
    {
      "regionId": "seattle-area-98340",
      "region": {
        "displayName": "Seattle Region",
        "postalCodeArea": {
          "regionCode": "US",
          "postalCodes": [
            {
              "begin": "98340"
            }
          ]
        }
      }
    },
    {
      "regionId": "co-de-states",
      "region": {
        "displayName": "Colorado and Delaware",
        "geoTargetArea": {
          "geotargetCriteriaIds": [
            "21138",
            "21141"
          ]
        }
      }
    }
  ]
}

คำตอบ

คำขอที่สำเร็จจะแสดงรายการออบเจ็กต์ region ใหม่

{
  "regions": [
    {
      "name": "accounts/{ACCOUNT_ID}/regions/seattle-area-98340",
      "displayName": "Seattle Region",
      "postalCodeArea": {
        "regionCode": "US",
        "postalCodes": [
          {
            "begin": "98340"
          }
        ]
      },
      "regionalInventoryEligible": true,
      "shippingEligible": true
    },
    {
      "name": "accounts/{ACCOUNT_ID}/regions/co-de-states",
      "displayName": "Colorado and Delaware",
      "geotargetArea": {
        "geotargetCriteriaIds": [
          "21138",
          "21141"
        ]
      },
      "regionalInventoryEligible": false,
      "shippingEligible": false
    }
  ]
}

ตัวอย่างต่อไปนี้แสดงวิธีสร้างภูมิภาคหลายภูมิภาคในคำขอแบบกลุ่ม

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.BatchCreateRegionsRequest;
import com.google.shopping.merchant.accounts.v1.BatchCreateRegionsResponse;
import com.google.shopping.merchant.accounts.v1.CreateRegionRequest;
import com.google.shopping.merchant.accounts.v1.Region;
import com.google.shopping.merchant.accounts.v1.Region.PostalCodeArea;
import com.google.shopping.merchant.accounts.v1.Region.PostalCodeArea.PostalCodeRange;
import com.google.shopping.merchant.accounts.v1.RegionsServiceClient;
import com.google.shopping.merchant.accounts.v1.RegionsServiceSettings;
import java.util.ArrayList;
import java.util.List;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to create multiple regions for a Merchant Center account. */
public class BatchCreateRegionsSample {

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

  public static void batchCreateRegions(Config config, List<String> regionIds) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    RegionsServiceSettings regionsServiceSettings =
        RegionsServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates parent to identify where to insert the regions.
    String parent = getParent(config.getAccountId().toString());

    // Calls the API and catches and prints any network failures/errors.
    try (RegionsServiceClient regionsServiceClient =
        RegionsServiceClient.create(regionsServiceSettings)) {

      List<CreateRegionRequest> requests = new ArrayList<>();
      for (String regionId : regionIds) {
        requests.add(
            CreateRegionRequest.newBuilder()
                .setParent(parent)
                .setRegionId(regionId)
                .setRegion(
                    Region.newBuilder()
                        .setDisplayName("Region " + regionId)
                        .setPostalCodeArea(
                            PostalCodeArea.newBuilder()
                                .setRegionCode("US")
                                .addPostalCodes(
                                    PostalCodeRange.newBuilder()
                                        .setBegin("10001")
                                        .setEnd("10282")
                                        .build())
                                .build())
                        .build())
                .build());
      }

      BatchCreateRegionsRequest request =
          BatchCreateRegionsRequest.newBuilder().setParent(parent).addAllRequests(requests).build();

      System.out.println("Sending Batch Create Regions request");
      BatchCreateRegionsResponse response = regionsServiceClient.batchCreateRegions(request);
      System.out.println("Inserted Regions Names below");
      // The last part of the region name will be the ID of the region.
      // Format: `accounts/{account}/region/{region}`
      response.getRegionsList().forEach(region -> System.out.println(region.getName()));

    } catch (Exception e) {
      System.out.println(e);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // The unique IDs of the regions to create.
    List<String> regionIds = new ArrayList<>();
    regionIds.add("REGION_1");
    regionIds.add("REGION_2");
    regionIds.add("REGION_3");
    regionIds.add("REGION_4");
    regionIds.add("REGION_5");

    batchCreateRegions(config, regionIds);
  }
}

อัปเดตภูมิภาคหลายภูมิภาค

ตัวอย่างนี้แสดงวิธีใช้ BatchUpdateRegions เพื่ออัปเดต displayName และ postalCodeArea สำหรับภูมิภาคที่มีอยู่ 2 ภูมิภาค คุณต้องระบุ region.name เพื่ออัปเดตภูมิภาคเป้าหมาย

ส่งคำขอ

สร้าง URL คำขอได้ดังนี้

POST https://merchantapi.googleapis.com/accounts/v1/accounts/{ACCOUNT_ID}/regions:batchUpdate

เนื้อหาคำขอจะมีรายการ requests แต่ละออบเจ็กต์ต้องระบุข้อมูล region ที่จะอัปเดต ช่อง region.name ต้องมีรหัสของภูมิภาคที่จะอัปเดต เช่น "98005" ระบุทรัพยากรเป็น name แทนที่จะเป็น accounts/{ACCOUNT_ID}/regions/name. การใส่ updateMask เพื่อระบุช่องที่จะเปลี่ยนแปลงเป็นตัวเลือก

{
  "requests": [
    {
      "region": {
        "name": "98005",
        "displayName": "Seattle Updated Region",
        "postalCodeArea": {
          "regionCode": "US",
          "postalCodes": [
            {
              "begin": "98330"
            }
          ]
        }
      },
      "updateMask": "displayName,postalCodeArea"
    },
    {
      "region": {
        "name": "07086",
        "displayName": "NewYork Updated Region",
        "postalCodeArea": {
          "regionCode": "US",
          "postalCodes": [
            {
              "begin": "11*"
            }
          ]
        }
      },
      "updateMask": "displayName,postalCodeArea"
    }
  ]
}

คำตอบ

คำขอที่สำเร็จจะแสดงรายการออบเจ็กต์ region ที่อัปเดต

{
  "regions": [
    {
      "name": "accounts/{ACCOUNT_ID}/regions/98005",
      "displayName": "Seattle Updated Region",
      "postalCodeArea": {
        "regionCode": "US",
        "postalCodes": [
          {
            "begin": "98330"
          }
        ]
      },
      "regionalInventoryEligible": true,
      "shippingEligible": true
    },
    {
      "name": "accounts/{ACCOUNT_ID}/regions/07086",
      "displayName": "NewYork Updated Region",
      "postalCodeArea": {
        "regionCode": "US",
        "postalCodes": [
          {
            "begin": "11*"
          }
        ]
      },
      "regionalInventoryEligible": true,
      "shippingEligible": true
    }
  ]
}

ตัวอย่างต่อไปนี้แสดงวิธีอัปเดตภูมิภาคหลายภูมิภาคในคำขอแบบกลุ่ม

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.protobuf.FieldMask;
import com.google.shopping.merchant.accounts.v1.BatchUpdateRegionsRequest;
import com.google.shopping.merchant.accounts.v1.BatchUpdateRegionsResponse;
import com.google.shopping.merchant.accounts.v1.Region;
import com.google.shopping.merchant.accounts.v1.RegionsServiceClient;
import com.google.shopping.merchant.accounts.v1.RegionsServiceSettings;
import com.google.shopping.merchant.accounts.v1.UpdateRegionRequest;
import java.util.ArrayList;
import java.util.List;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to update multiple regions for a Merchant Center account. */
public class BatchUpdateRegionsSample {

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

  private static String getRegionName(String accountId, String regionId) {
    return String.format("accounts/%s/regions/%s", accountId, regionId);
  }

  public static void batchUpdateRegions(Config config, List<String> regionIds) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    RegionsServiceSettings regionsServiceSettings =
        RegionsServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates parent to identify where to update the regions.
    String parent = getParent(config.getAccountId().toString());
    String accountId = config.getAccountId().toString();

    // Calls the API and catches and prints any network failures/errors.
    try (RegionsServiceClient regionsServiceClient =
        RegionsServiceClient.create(regionsServiceSettings)) {

      List<UpdateRegionRequest> requests = new ArrayList<>();
      for (String regionId : regionIds) {
        requests.add(
            UpdateRegionRequest.newBuilder()
                .setRegion(
                    Region.newBuilder()
                        .setName(getRegionName(accountId, regionId))
                        .setDisplayName("Updated Region " + regionId)
                        .build())
                .setUpdateMask(FieldMask.newBuilder().addPaths("display_name").build())
                .build());
      }

      BatchUpdateRegionsRequest request =
          BatchUpdateRegionsRequest.newBuilder().setParent(parent).addAllRequests(requests).build();

      System.out.println("Sending Batch Update Regions request");
      BatchUpdateRegionsResponse response = regionsServiceClient.batchUpdateRegions(request);
      System.out.println("Updated Regions Names below");
      // The last part of the region name will be the ID of the region.
      // Format: `accounts/{account}/region/{region}`
      response.getRegionsList().forEach(region -> System.out.println(region.getName()));

    } catch (Exception e) {
      System.out.println(e);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // The unique IDs of the regions to update.
    List<String> regionIds = new ArrayList<>();
    regionIds.add("REGION_1");
    regionIds.add("REGION_2");
    regionIds.add("REGION_3");
    regionIds.add("REGION_4");
    regionIds.add("REGION_5");

    batchUpdateRegions(config, regionIds);
  }
}

ลบภูมิภาคหลายภูมิภาค

คุณสามารถลบภูมิภาคหลายภูมิภาคได้ในการเรียกครั้งเดียว

ส่งคำขอ

ตัวอย่างนี้แสดงวิธีใช้ BatchDeleteRegions เพื่อลบภูมิภาค 2 ภูมิภาคในการเรียกครั้งเดียว

POST
https://merchantapi.googleapis.com/accounts/v1/accounts/{ACCOUNT_ID}/regions:batchDelete

เนื้อหาคำขอจะมีรายการ requests ซึ่งแต่ละออบเจ็กต์จะระบุ name (ไม่มี "accounts/{ACCOUNT_ID}/regions/") ของภูมิภาคที่จะลบ

{
  "requests":
   [
    {
      "name": "98005"
    },
    {
      "name": "07086"
    }
   ]
}

คำตอบ

คำขอที่สำเร็จจะแสดงเนื้อหาการตอบกลับที่ว่างเปล่า ซึ่งบ่งบอกว่าภูมิภาคที่ระบุถูกลบแล้ว (หรือไม่มีอยู่)

{}

ตัวอย่างต่อไปนี้แสดงวิธีลบภูมิภาคหลายภูมิภาคในคำขอแบบกลุ่ม

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.BatchDeleteRegionsRequest;
import com.google.shopping.merchant.accounts.v1.DeleteRegionRequest;
import com.google.shopping.merchant.accounts.v1.RegionsServiceClient;
import com.google.shopping.merchant.accounts.v1.RegionsServiceSettings;
import java.util.ArrayList;
import java.util.List;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to delete multiple regions for a Merchant Center account. */
public class BatchDeleteRegionsSample {

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

  private static String getRegionName(String accountId, String regionId) {
    return String.format("accounts/%s/regions/%s", accountId, regionId);
  }

  public static void batchDeleteRegions(Config config, List<String> regionIds) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    RegionsServiceSettings regionsServiceSettings =
        RegionsServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates parent to identify where to delete the regions.
    String parent = getParent(config.getAccountId().toString());
    String accountId = config.getAccountId().toString();

    // Calls the API and catches and prints any network failures/errors.
    try (RegionsServiceClient regionsServiceClient =
        RegionsServiceClient.create(regionsServiceSettings)) {

      List<DeleteRegionRequest> requests = new ArrayList<>();
      for (String regionId : regionIds) {
        requests.add(
            DeleteRegionRequest.newBuilder().setName(getRegionName(accountId, regionId)).build());
      }

      BatchDeleteRegionsRequest request =
          BatchDeleteRegionsRequest.newBuilder().setParent(parent).addAllRequests(requests).build();

      System.out.println("Sending Batch Delete Regions request");
      regionsServiceClient.batchDeleteRegions(request);
      System.out.println("Regions deleted successfully");

    } catch (Exception e) {
      System.out.println(e);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // The unique IDs of the regions to delete.
    List<String> regionIds = new ArrayList<>();
    regionIds.add("REGION_1");
    regionIds.add("REGION_2");
    regionIds.add("REGION_3");
    regionIds.add("REGION_4");
    regionIds.add("REGION_5");

    batchDeleteRegions(config, regionIds);
  }
}

ข้อจำกัด

โปรดคำนึงถึงกฎต่อไปนี้ก่อนเริ่มต้น

  • การดำเนินการแบบอะตอมมิก: คำขอแบบกลุ่มเป็นการดำเนินการแบบอะตอมมิก หากการดำเนินการใดการดำเนินการหนึ่ง ภายในกลุ่มล้มเหลว (เช่น สร้างภูมิภาคหนึ่งภูมิภาคไม่สำเร็จ) กลุ่มทั้งหมดจะล้มเหลว และจะไม่มีการเปลี่ยนแปลงใดๆ API จะแสดงข้อผิดพลาดที่ระบุรายละเอียดสาเหตุของความล้มเหลว
  • ขีดจำกัดของกลุ่ม: คำขอแบบกลุ่มแต่ละรายการสามารถมีการดำเนินการเกี่ยวกับภูมิภาคได้สูงสุด 100 รายการ
  • โควต้า: ปลายทางเหล่านี้ใช้กลุ่มโควต้าเดียวกับปลายทางที่มี การดำเนินการเดียว (regions.create, regions.delete, regions.update)

ข้อผิดพลาดและปัญหาที่พบบ่อย

ต่อไปนี้คือข้อผิดพลาดที่พบบ่อยและวิธีแก้ไข

"The number of requests in a batch is too large"

ข้อผิดพลาดนี้เกิดขึ้นหากจำนวนการดำเนินการในอาร์เรย์คำขอเกินขีดจำกัดที่ 100 รายการ

"error":
  {
    "code": 400,
    "message": "The number of requests in a batch is too large.",
    "status": "INVALID_ARGUMENT"
  }

หากต้องการแก้ไข ให้แบ่งการดำเนินการออกเป็นคำขอแบบกลุ่มหลายรายการที่มีการดำเนินการไม่เกิน 100 รายการ

ไม่มีข้อมูลในช่องที่ต้องระบุ

ข้อผิดพลาดนี้เกิดขึ้นเมื่อไม่มีข้อมูลในช่องที่ต้องระบุ ข้อความแสดงข้อผิดพลาดจะระบุพารามิเตอร์ที่ขาดหายไป

ข้อความแสดงข้อผิดพลาดมีดังนี้

  • สำหรับ batchCreate: [regionId] Required parameter: regionId
  • สำหรับ batchUpdate: [region.name] Required field not provided.
  • สำหรับ batchDelete: [name] Required parameter: name

หากต้องการแก้ไข ให้ตรวจสอบว่ามีข้อมูลในช่องที่ต้องระบุทั้งหมดในการดำเนินการแต่ละรายการ เช่น ทุกรายการในคำขอ batchUpdate ต้องมี region.name การโพสต์คำขอต่อไปนี้จะทำให้เกิดข้อผิดพลาด

{
  "requests":
  [
    {
      "region":
        {
          "displayName": "An update without a region name"
        },
        "updateMask": "displayName"
    }
  ]
}

"Region with specified ID already exists"

ข้อผิดพลาดจะเกิดขึ้นหากคุณพยายามสร้างภูมิภาคที่มี regionId ที่มีอยู่แล้ว

ข้อความแสดงข้อผิดพลาดคือ [regionId] Region with specified id already exists.

หากต้องการแก้ไข ให้ตรวจสอบว่าค่า regionId ทั้งหมดไม่ซ้ำกันภายในกลุ่มและไม่ขัดแย้งกับภูมิภาคที่มีอยู่

"Duplicate value found for field region.name or regionId found"

ข้อผิดพลาดจะเกิดขึ้นหากคุณพยายามสร้างหรืออัปเดตภูมิภาคหลายภูมิภาคที่มีรหัสเดียวกันภายในคำขอแบบกลุ่มเดียว

ข้อความแสดงข้อผิดพลาดคือ Duplicate value found for field {fieldName} in this batch request with value {duplicated_value}.

หากต้องการแก้ไข ให้ตรวจสอบว่าค่า regionId ทั้งหมด (สำหรับ batchCreate) หรือ region.name (สำหรับ batchUpdate) ไม่ซ้ำกันภายในคำขอแบบกลุ่มเดียว

"Item not found"

เมื่อใช้ batchUpdate หากภูมิภาคใดภูมิภาคหนึ่งที่ระบุในคำขอไม่มีอยู่ กลุ่มทั้งหมดจะล้มเหลวโดยมีข้อผิดพลาด 404 NOT_FOUND ซึ่งแตกต่างจาก batchDelete ที่จะดำเนินการสำเร็จสำหรับภูมิภาคที่ไม่มีอยู่

"error": {
    "code": 404,
    "message": "item not found",
    "status": "NOT_FOUND"
}

หากต้องการแก้ไข ให้ตรวจสอบว่าภูมิภาคทั้งหมดที่คุณพยายามอัปเดตมีอยู่ก่อนส่งคำขอ