Werken met
Werken metWooCommerce

WooCommerce

Met de WooCommerce-extensie kunnen we WooCommerce-productgegevens ophalen via GraphQL.

WooCommerce-producttypen

Het veld woocommerceProducts retourneert een WooCommerceProductUnion-type, samengesteld uit de 4 mogelijke producttypen:

  • WooCommerceSimpleProduct
  • WooCommerceExternalProduct
  • WooCommerceGroupProduct
  • WooCommerceVariableProduct

Afhankelijk van elk type zijn er verschillende velden beschikbaar om op te vragen.

WooCommerce-productinterfaces

Omdat 2 verschillende producttypen (en ook productvariaties) sommige velden kunnen delen, zijn deze toegevoegd aan interfaces die elk producttype al dan niet kan implementeren.

Zo is Shippable bedoeld voor producten die verzending ondersteunen, Priceable voor producten met een prijs (een variatieprofuct heeft geen price-veld; dat wordt via de variaties aangeboden), Taxable voor producten met belastinginstellingen, enzovoort.

InterfaceBeschrijving
WooCommerceProductVelden gedeeld door alle producttypen (d.w.z. geĆÆmplementeerd door alle 4 typen)
WooCommerceProductOrProductVariationVelden gedeeld door alle producttypen en ook productvariaties
WooCommerceCrossSellableProductProducten die het definiƫren van "cross-sell"-producten ondersteunen
WooCommerceDownloadableProductOrProductVariationProducten (en productvariaties) die downloadbare bestanden ondersteunen
WooCommercePriceableProductOrProductVariationProducten (en productvariaties) met een price-veld
WooCommerceShippableProductOrProductVariationProducten (en productvariaties) die verzendinstellingen ondersteunen
WooCommerceTaxableProductProducten die belastinginstellingen ondersteunen
WooCommerceWithStockManagementProductOrProductVariationProducten (en productvariaties) die voorraadbeheer ondersteunen

Productgegevens ophalen

De volgende query haalt gegevens op voor alle producttypen en bevraagt alle velden per producttype:

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
}

Specifieke producttypen opvragen

Je kunt specifieke producttypen opvragen door het producttype te filteren in het veld woocommerceProducts:

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

Of door het bijbehorende veld voor dat type te gebruiken:

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

Eenvoudige producten

Deze query haalt gegevens op voor eenvoudige producten:

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

Externe producten

Deze query haalt gegevens op voor externe producten, inclusief de externe URL en knoptekst:

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

Groepsproducten

Deze query haalt gegevens op voor groepsproducten, inclusief de onderliggende producten:

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

Variabele producten

Deze query haalt gegevens op voor variabele producten, inclusief de variaties:

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

Voor variabele producten kun je de variaties ook rechtstreeks opvragen:

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

Opvragen met filters

Je kunt producten filteren, sorteren en pagineren op basis van verschillende 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
    }
  }
}