Working with
Working withWooCommerce

WooCommerce

Using the WooCommerce extension, we can retrieve WooCommerce products data through GraphQL.

WooCommerce Product Types

Field woocommerceProducts returns a WooCommerceProductUnion type, composed of the 4 possible product types:

  • WooCommerceSimpleProduct
  • WooCommerceExternalProduct
  • WooCommerceGroupProduct
  • WooCommerceVariableProduct

Depending on each type, there will be different fields to query.

WooCommerce Product Interfaces

Since 2 different product types (and also product variations) might share some fields, these were added to Interfaces that each of the product types may or may not implement.

For instance, Shippable is for products that support shipping, Priceable for products that have a price (a variation product does not have a price field, that is provided via its variations), Taxable for products that support tax settings, and so on.

InterfaceDescription
WooCommerceProductFields shared by all product types (i.e. implemented by all 4 types)
WooCommerceProductOrProductVariationFields shared by both all product types and also product variations
WooCommerceCrossSellableProductProducts that support defining "cross-sell" products
WooCommerceDownloadableProductOrProductVariationProducts (and product variations) that support downloadable files
WooCommercePriceableProductOrProductVariationProducts (and product variations) that have a price field
WooCommerceShippableProductOrProductVariationProducts (and product variations) that support shipping settings
WooCommerceTaxableProductProducts that support tax settings
WooCommerceWithStockManagementProductOrProductVariationProducts (and product variations) that support stock management

Fetching Product Data

The following query retrieves data for all product types, querying all fields for each product type:

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
}

Querying specific product types

You can query specific product types by filtering the product type in field woocommerceProducts:

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

Or by using the corresponding field for that type:

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

Simple Products

This query fetches data for simple products:

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

External Products

This query fetches data for external products, including the external URL and button text:

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

Group Products

This query fetches data for group products, including the children:

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

Variable Products

This query fetches data for variable products, including the variations:

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

For variable products, you can also query the variations directly:

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

Querying with filters

You can filter, sort and paginate products by various criteria:

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