Praca z
Praca zWooCommerce

WooCommerce

Używając rozszerzenia WooCommerce, możemy pobierać dane produktów WooCommerce za pomocą GraphQL.

Typy produktów WooCommerce

Pole woocommerceProducts zwraca typ WooCommerceProductUnion, składający się z 4 możliwych typów produktów:

  • WooCommerceSimpleProduct
  • WooCommerceExternalProduct
  • WooCommerceGroupProduct
  • WooCommerceVariableProduct

W zależności od każdego typu, dostępne będą różne pola do odpytania.

Interfejsy produktów WooCommerce

Ponieważ 2 różne typy produktów (a także warianty produktów) mogą współdzielić niektóre pola, zostały one dodane do interfejsów, które każdy typ produktu może lub nie implementować.

Na przykład Shippable dotyczy produktów obsługujących wysyłkę, Priceable dla produktów posiadających cenę (wariant produktu nie ma pola price, które jest dostarczane przez jego warianty), Taxable dla produktów obsługujących ustawienia podatkowe i tak dalej.

InterfejsOpis
WooCommerceProductPola wspólne dla wszystkich typów produktów (tj. implementowane przez wszystkie 4 typy)
WooCommerceProductOrProductVariationPola wspólne dla wszystkich typów produktów, a także wariantów produktów
WooCommerceCrossSellableProductProdukty obsługujące definiowanie produktów „sprzedaży krzyżowej"
WooCommerceDownloadableProductOrProductVariationProdukty (i warianty produktów) obsługujące pliki do pobrania
WooCommercePriceableProductOrProductVariationProdukty (i warianty produktów) posiadające pole price
WooCommerceShippableProductOrProductVariationProdukty (i warianty produktów) obsługujące ustawienia wysyłki
WooCommerceTaxableProductProdukty obsługujące ustawienia podatkowe
WooCommerceWithStockManagementProductOrProductVariationProdukty (i warianty produktów) obsługujące zarządzanie stanem magazynowym

Pobieranie danych produktów

Poniższa query pobiera dane dla wszystkich typów produktów, odpytując wszystkie pola dla każdego typu:

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
}

Odpytywanie określonych typów produktów

Możesz odpytywać określone typy produktów, filtrując typ produktu w polu woocommerceProducts:

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

Lub używając odpowiedniego pola dla danego typu:

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

Produkty proste

Ta query pobiera dane produktów prostych:

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

Produkty zewnętrzne

Ta query pobiera dane produktów zewnętrznych, w tym zewnętrzny URL i tekst przycisku:

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

Produkty grupowe

Ta query pobiera dane produktów grupowych, w tym produkty podrzędne:

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

Produkty zmienne

Ta query pobiera dane produktów zmiennych, w tym warianty:

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

W przypadku produktów zmiennych możesz również odpytywać warianty bezpośrednio:

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

Odpytywanie z filtrami

Możesz filtrować, sortować i stronicować produkty według różnych kryteriów:

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