操作ガイド
操作ガイドWooCommerce

WooCommerce

WooCommerce拡張機能を使用することで、GraphQL経由でWooCommerceの商品データを取得できます。

WooCommerce商品タイプ

フィールドwoocommerceProductsWooCommerceProductUnion型を返し、4つの商品タイプで構成されています。

  • WooCommerceSimpleProduct
  • WooCommerceExternalProduct
  • WooCommerceGroupProduct
  • WooCommerceVariableProduct

各タイプによって、クエリできるフィールドが異なります。

WooCommerce商品インターフェース

2つの異なる商品タイプ(および商品バリエーション)が一部のフィールドを共有する場合があるため、それらは各商品タイプが実装するかどうかを選択できるインターフェースに追加されています。

たとえば、Shippableは配送に対応した商品、Priceableは価格を持つ商品(バリエーション商品にはpriceフィールドがなく、バリエーション経由で提供されます)、Taxableは税設定に対応した商品、などです。

インターフェース説明
WooCommerceProductすべての商品タイプで共有されるフィールド(4タイプすべてで実装)
WooCommerceProductOrProductVariationすべての商品タイプおよび商品バリエーションで共有されるフィールド
WooCommerceCrossSellableProduct「クロスセル」商品の定義をサポートする商品
WooCommerceDownloadableProductOrProductVariationダウンロードファイルをサポートする商品(および商品バリエーション)
WooCommercePriceableProductOrProductVariationpriceフィールドを持つ商品(および商品バリエーション)
WooCommerceShippableProductOrProductVariation配送設定をサポートする商品(および商品バリエーション)
WooCommerceTaxableProduct税設定をサポートする商品
WooCommerceWithStockManagementProductOrProductVariation在庫管理をサポートする商品(および商品バリエーション)

商品データの取得

以下のクエリは、すべての商品タイプのデータを取得し、各商品タイプのすべてのフィールドをクエリします。

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
}

特定の商品タイプのクエリ

フィールドwoocommerceProductsで商品タイプをフィルタリングすることで、特定の商品タイプをクエリできます。

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

または、各タイプに対応するフィールドを使用することもできます。

  • woocommerceSimpleProduct and woocommerceSimpleProducts
  • woocommerceExternalProduct and woocommerceExternalProducts
  • woocommerceGroupProduct and woocommerceGroupProducts
  • woocommerceVariableProduct and woocommerceVariableProducts

シンプル商品

このクエリはシンプル商品のデータを取得します。

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

外部商品

このクエリは外部商品のデータを取得します(外部URLとボタンテキストを含む)。

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

グループ商品

このクエリはグループ商品のデータを取得します(子商品を含む)。

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

バリアブル商品

このクエリはバリアブル商品のデータを取得します(バリエーションを含む)。

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

バリアブル商品の場合、バリエーションを直接クエリすることもできます。

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
      }
    }
  }
}

フィルターを使ったクエリ

さまざまな条件で商品をフィルタリング、ソート、ページネーションできます。

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
    }
  }
}