Làm việc với
Làm việc vớiWooCommerce

WooCommerce

Sử dụng tiện mở rộng WooCommerce, chúng ta có thể lấy dữ liệu sản phẩm WooCommerce thông qua GraphQL.

Các loại sản phẩm WooCommerce

Trường woocommerceProducts trả về kiểu WooCommerceProductUnion, bao gồm 4 loại sản phẩm có thể có:

  • WooCommerceSimpleProduct
  • WooCommerceExternalProduct
  • WooCommerceGroupProduct
  • WooCommerceVariableProduct

Tùy thuộc vào từng loại, sẽ có các trường khác nhau để truy vấn.

Các Interface sản phẩm WooCommerce

Vì 2 loại sản phẩm khác nhau (và cả các biến thể sản phẩm) có thể dùng chung một số trường, các trường này đã được thêm vào các Interface mà mỗi loại sản phẩm có thể triển khai hoặc không.

Ví dụ, Shippable dành cho các sản phẩm hỗ trợ vận chuyển, Priceable cho các sản phẩm có giá (sản phẩm biến thể không có trường price, trường đó được cung cấp qua các biến thể của nó), Taxable cho các sản phẩm hỗ trợ cài đặt thuế, v.v.

InterfaceMô tả
WooCommerceProductCác trường dùng chung cho tất cả loại sản phẩm (tức là được triển khai bởi cả 4 loại)
WooCommerceProductOrProductVariationCác trường dùng chung cho tất cả loại sản phẩm và cả biến thể sản phẩm
WooCommerceCrossSellableProductCác sản phẩm hỗ trợ định nghĩa sản phẩm "bán kèm"
WooCommerceDownloadableProductOrProductVariationCác sản phẩm (và biến thể sản phẩm) hỗ trợ tệp tải xuống
WooCommercePriceableProductOrProductVariationCác sản phẩm (và biến thể sản phẩm) có trường price
WooCommerceShippableProductOrProductVariationCác sản phẩm (và biến thể sản phẩm) hỗ trợ cài đặt vận chuyển
WooCommerceTaxableProductCác sản phẩm hỗ trợ cài đặt thuế
WooCommerceWithStockManagementProductOrProductVariationCác sản phẩm (và biến thể sản phẩm) hỗ trợ quản lý kho hàng

Lấy dữ liệu sản phẩm

Query sau đây lấy dữ liệu cho tất cả các loại sản phẩm, truy vấn tất cả các trường cho từng loại sản phẩm:

query FetchWooCommerceData
{
  # Single product by ID
  productByID: woocommerceProduct(by: { id: 43 }) {
    __typename
    ...WooCommerceSimpleProductFields
    ...WooCommerceGroupProductFields
    ...WooCommerceExternalProductFields
    ...WooCommerceVariableProductFields
  }
 
  # Single product by slug
  productBySlug: woocommerceProduct(by: { slug: "iphone-15-pro" }) {
    __typename
    id
    name
    slug
    url
    urlPath
    sku
  }
 
  # Single product by SKU
  productBySku: woocommerceProduct(by: { sku: "IPHONE15PRO" }) {
    __typename
    id
    name
    slug
    url
    urlPath
    sku
  }
 
  # List of products
  woocommerceProducts {
    __typename
    ...WooCommerceSimpleProductFields
    ...WooCommerceGroupProductFields
    ...WooCommerceExternalProductFields
    ...WooCommerceVariableProductFields
  }
  woocommerceProductsCount
}
 
# ----------------------------------------------------------------------
# Fragments
# ----------------------------------------------------------------------
 
fragment WooCommerceSimpleProductFields on WooCommerceSimpleProduct {
  # Specific fields for this type
  # (empty)
 
  # Common fields
  ...WooCommerceProductOrProductVariationInterfaceFields
  ...WooCommerceIdentifiableObjectInterfaceFields
  ...WooCommerceProductInterfaceFields
  ...WooCommerceCrossSellableProductInterfaceFields
  ...WooCommerceDownloadableProductOrProductVariationInterfaceFields
  ...WooCommercePriceableProductOrProductVariationInterfaceFields
  ...WooCommerceShippableProductOrProductVariationInterfaceFields
  ...WooCommerceTaxableProductInterfaceFields
  ...WooCommerceWithStockManagementProductOrProductVariationInterfaceFields
}
 
fragment WooCommerceGroupProductFields on WooCommerceGroupProduct {
  # Specific fields for this type
  hasChildren
  childrenCount
  minPrice
  maxPrice
  minPriceFormatted
  maxPriceFormatted
  children {
    id
    name
    slug
    sku
  }
 
  # Common fields
  ...WooCommerceProductOrProductVariationInterfaceFields
  ...WooCommerceIdentifiableObjectInterfaceFields
  ...WooCommerceProductInterfaceFields
}
 
fragment WooCommerceExternalProductFields on WooCommerceExternalProduct {
  # Specific fields for this type
  externalURL
  buttonText
 
  # Common fields
  ...WooCommerceProductOrProductVariationInterfaceFields
  ...WooCommerceIdentifiableObjectInterfaceFields
  ...WooCommerceProductInterfaceFields
  ...WooCommercePriceableProductOrProductVariationInterfaceFields
  ...WooCommerceTaxableProductInterfaceFields
}
 
fragment WooCommerceVariableProductFields on WooCommerceVariableProduct {
  # Specific fields for this type
  hasVariations
  variationsCount
  minPrice
  maxPrice
  minRegularPrice
  maxRegularPrice
  minSalePrice
  maxSalePrice
  priceRange
  variations {
    id
    name
    slug
    sku
  }
  defaultAttributes {
    taxonomy
    termSlug
    termObject {
      id
      name
      slug
    }
  }
 
  # Common fields
  ...WooCommerceProductOrProductVariationInterfaceFields
  ...WooCommerceIdentifiableObjectInterfaceFields
  ...WooCommerceProductInterfaceFields
  ...WooCommerceCrossSellableProductInterfaceFields
  ...WooCommerceTaxableProductInterfaceFields
}
 
# Interfaces
# ----------------------------------------------------------------------
 
fragment WooCommerceCrossSellableProductInterfaceFields on WooCommerceCrossSellableProduct {
  crossSellIDs
  crossSells {
    id
    name
    slug
    sku
  }
}
 
fragment WooCommerceDownloadableProductOrProductVariationInterfaceFields on WooCommerceDownloadableProductOrProductVariation {
  isDownloadable
  downloadLimit
  downloadExpiry
  downloads
  downloadsCount
}
 
fragment WooCommercePriceableProductOrProductVariationInterfaceFields on WooCommercePriceableProductOrProductVariation {
  price
  priceFormatted
  regularPrice
  regularPriceFormatted
  salePrice
  salePriceFormatted
  onSale
  dateOnSaleFrom
  dateOnSaleFromStr
  formattedDateOnSaleFromStr: dateOnSaleFromStr(format: "d/m/Y H:i:s")
  dateOnSaleTo
  dateOnSaleToStr
  formattedDateOnSaleToStr: dateOnSaleToStr(format: "d/m/Y H:i:s")
}
 
fragment WooCommerceShippableProductOrProductVariationInterfaceFields on WooCommerceShippableProductOrProductVariation {
  isVirtual
  weight
  length
  width
  height
  dimensions
  shippingClassID
  shippingClass {
    id
    name
    slug
    description
    count
  }
}
 
fragment WooCommerceTaxableProductInterfaceFields on WooCommerceTaxableProduct {
  taxStatus
  taxClass
}
 
fragment WooCommerceWithStockManagementProductOrProductVariationInterfaceFields on WooCommerceWithStockManagementProductOrProductVariation {
  manageStock
  stockQuantity
  stockStatus
  backorders
  backordersAllowed
  backordered
  soldIndividually
  lowStockThreshold
}
 
fragment WooCommerceProductOrProductVariationInterfaceFields on WooCommerceProductOrProductVariation {
  name
  description
  shortDescription
  sku
  globalUniqueID
  isPurchasable
  image {
    id
    src
    altText
    title
    caption
  }
  imageID
  catalogVisibility
  status
  date
  dateStr
  formattedDateStr: dateStr(format: "d/m/Y H:i:s")
  modifiedDate
  modifiedDateStr
  formattedModifiedDateStr: modifiedDateStr(format: "d/m/Y H:i:s")
}
 
fragment WooCommerceIdentifiableObjectInterfaceFields on IdentifiableObject {
  id
}
 
fragment WooCommerceProductInterfaceFields on WooCommerceProduct {
  url
  urlPath
  slug
  featured
  totalSales
  reviewsAllowed
  averageRating
  ratingCount
  upsellIDs
  upsells {
    id
    name
    slug
    sku
  }
  upsellsCount
  crossSellsCount
  purchaseNote
  menuOrder
  type
  isVisible
  categories {
    id
    name
    slug
  }
  categoriesCount
  tags {
    id
    name
    slug
  }
  tagsCount
  brands {
    id
    name
    slug
  }
  brandsCount
  galleryImages {
    id
    src
    altText
    title
    caption
  }
  galleryImagesCount
  attributes {
    name
    taxonomyObject {
      id
      name
      slug
      options {
        id
        name
        slug
      }
    }
    options
    optionTaxonomyTermObjects {
      id
      name
      slug
    }
    position
    isVisible
    isVariation
    isTaxonomy
  }
  reviews {
    id
    content
    author
  }
  reviewsCount
}

Truy vấn các loại sản phẩm cụ thể

Bạn có thể truy vấn các loại sản phẩm cụ thể bằng cách lọc loại sản phẩm trong trường woocommerceProducts:

query FilterProductsByType {
  woocommerceProducts(filter: { types: [simple, external, variable] }) {
    __typename
    ... on WooCommerceProduct {
      id
      name
      sku
    }
    ... on WooCommercePriceableProductOrProductVariation {
      price
    }
  }
  woocommerceProductsCount(filter: { types: [simple, external, variable] })
}

Hoặc bằng cách sử dụng trường tương ứng cho loại đó:

  • woocommerceSimpleProductwoocommerceSimpleProducts
  • woocommerceExternalProductwoocommerceExternalProducts
  • woocommerceGroupProductwoocommerceGroupProducts
  • woocommerceVariableProductwoocommerceVariableProducts

Sản phẩm đơn giản

Query này lấy dữ liệu cho các sản phẩm đơn giản:

query FetchSimpleProducts {
  woocommerceSimpleProducts {
    __typename
    id
    name
    sku
  }
  woocommerceSimpleProductsCount
}

Sản phẩm bên ngoài

Query này lấy dữ liệu cho các sản phẩm bên ngoài, bao gồm URL bên ngoài và văn bản nút:

query FetchExternalProducts {
  woocommerceExternalProducts {
    __typename
    id
    name
    sku
    externalURL
    buttonText
  }
  woocommerceExternalProductsCount
}

Sản phẩm nhóm

Query này lấy dữ liệu cho các sản phẩm nhóm, bao gồm cả các sản phẩm con:

query FetchGroupProducts {
  woocommerceGroupProducts {
    __typename
    id
    name
    sku
    children {
      id
      name
    }
    childrenCount
  }
  woocommerceGroupProductsCount  
}

Sản phẩm biến thể

Query này lấy dữ liệu cho các sản phẩm biến thể, bao gồm các biến thể:

query FetchVariableProducts {
  woocommerceVariableProducts {
    __typename
    id
    name
    sku
    variations {
      id
      name
      price
    }
    variationsCount
  }
  woocommerceVariableProductsCount
}

Đối với sản phẩm biến thể, bạn cũng có thể truy vấn trực tiếp các biến thể:

query GetProductVariations {
  woocommerceProductVariations {
    id
    name
    sku
    price
    parent {
      id
      name
      sku
    }
    priceFormatted
    regularPrice
    regularPriceFormatted
    salePrice
    salePriceFormatted
    stockStatus
    stockQuantity
    attributes {
      taxonomy
      termSlug
      termObject {
        id
        name
        slug
      }
    }
  }
}

Truy vấn với bộ lọc

Bạn có thể lọc, sắp xếp và phân trang sản phẩm theo nhiều tiêu chí khác nhau:

query GetFilteredProducts {
  woocommerceProducts(pagination: { limit: -1 }, filter: { 
    types: [simple, external], 
    visibility: visible, 
    categoriesBy: { ids: [81] },
    tagsBy: { slugs: ["apple"] },
  }) {
    id
    name
    status
    type
    isVisible
    catalogVisibility
    categories(sort: { by: ID, order: DESC }) {
      id
      name
      slug
    }
    tags {
      id
      name
      slug
    }
  }
}