Skip to content

Product Variants

Đào sâu tính năng. Định danh dịch vụ và bề mặt async nằm trong Tổng quan CommerceAPI Events.

Một ProductVariant đại diện cho một cấu hình bán được cụ thể của sản phẩm (ví dụ kích cỡ, màu sắc). Mỗi variant có ProductInfo riêng, identifier (SKU, barcode), dữ liệu pricing tùy chọn, và MetaLinks (hình ảnh).

Tổng quan

Thuộc tínhGiá trị
Thực thểProductVariant, ProductInfo, ProductIdentifier, MetaLink
Base path/product-variants (live spec: /v1/api/commerce/doc/openapi.json)
Type cấu trúcProductVariant.type — xem Domain Model §4.1
Pricingcatalog lưu dữ liệu pricing; tính toán nằm trong @nx/pricing

Mô hình thực thể

Xem Domain Model §3.2. Bảng trường được duy trì ở đó.

REST Endpoints

Tham khảo đầy đủ: OpenAPI trực tiếp tại /v1/api/commerce/doc/openapi.json. Route tùy biến:

MethodPathMục đích
POST/product-variants/aggregateTạo variant + info + identifiers + MetaLinks
PATCH/product-variants/{id}/aggregateCập nhật variant + info + identifiers một cách atomic

Phân giải identifier

GET /product-variants/{id} phân giải bằng:

  1. Khớp ID trực tiếp
  2. Khớp trường slug

ProductVariantService

File: src/services/product-variant.service.ts

Methods

MethodMục đích
findByIdentifier({ identifier, filter?, transaction? })Phân giải theo ID hoặc slug
createAggregate({ data, transaction? })Tạo variant + info + identifiers + MetaLinks
updateByIdProductVariantAggregate({ id, data, transaction? })Cập nhật variant + info + identifiers

Request createAggregate()

typescript
{
  // các trường ProductVariantInsertSchema
  productId: string;
  status?: string;

  // riêng cho aggregate
  info: ProductInfoInsertSchema;          // bắt buộc — { name: { en, vi }, description: { en, vi } }
  pricing?: PricingSchema;                // tùy chọn — pricing cấp variant
  metaLinks?: MetaLinkUpdateSchema[];     // tùy chọn — hình ảnh/asset (upsert theo ID)
  barcode?: string;                       // tùy chọn — tạo ProductIdentifier (scheme: BARCODE)
  sku?: string;                           // tùy chọn — tạo ProductIdentifier (scheme: SKU)
}

Nó làm gì:

  1. Tạo hàng ProductVariant với metadata { merchantId, categoryId } từ Product cha
  2. Tạo ProductInfo cho variant
  3. Tạo ProductIdentifier với scheme SYSTEM (tự sinh)
  4. Nếu sku được cung cấp: tạo ProductIdentifier với scheme SKU
  5. Nếu barcode được cung cấp: tạo ProductIdentifier với scheme BARCODE
  6. Upsert các bản ghi MetaLink nếu được cung cấp

Request updateByIdProductVariantAggregate()

typescript
{
  // các trường ProductVariantUpdateSchema (partial)
  info?: ProductInfoInsertSchema;     // cập nhật partial
  pricing?: PricingSchema;
  barcode?: string;                   // upsert identifier BARCODE
  sku?: string;                       // upsert identifier SKU
}

Sự kiện

KênhTín hiệuKhi nàoConsumer
CDC (Debezium)public.ProductVariantkhi ghi hàngInventory (seed InventoryItem cho type STOCKABLE_SET), Pricing (init fare từ dữ liệu pricing), Search (index)

Commerce không emit cấp ứng dụng các topic Kafka product-variant.created/.updated — enqueue Kafka cập nhật variant (_enqueueProductVariantUpdated) đã bị comment out trong product-variant.service.ts. Lan truyền được điều khiển bởi CDC. Xem API Events.

Xóa

Được xử lý bởi DeletionPolicyService.deleteProductVariantById():

Điều kiệnHành vi
Variant có lịch sử bán (orders)HTTP 400 — archive thay vì xóa
Variant cuối cùng của product + canDeleteLastVariant=falseHTTP 409
Ngược lạiĐã xóa

Không có generateCombinations(), cartesianProduct(), hay endpoint sinh variant tự động. Variant được tạo từng cái một qua endpoint aggregate.

Trang liên quan

TrangMô tả
Tổng quan CommerceĐịnh danh dịch vụ + catalog
Domain ModelBảng trường Variant + ProductVariantTypes
ProductsQuản lý product cha
Pricing trong CommerceLuồng dữ liệu pricing variant
ADR-0003Mô hình bộ phân biệt variant type

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.