Product update webhook: how to tell if product has only the default variant

Solved
tunisoft
Excursionist
13 3 0

Hi,

When I receive the product details on my webhook endpoint, I can't tell if the product has only the default variant of has one custom variant

Default variant only

 

/*variants:*/ [
  {
    id: XXX,
    product_id: XXXXX,
    title: 'Default Title',
    price: '7.500',
    sku: '',
    position: 1,
    inventory_policy: 'deny',
    compare_at_price: null,
    fulfillment_service: 'manual',
    inventory_management: null,
    option1: 'Default Title',
    option2: null,
    option3: null,
    created_at: '2021-07-25T05:55:20-04:00',
    updated_at: '2021-07-25T05:57:38-04:00',
    taxable: true,
    barcode: '',
    grams: 0,
    image_id: null,
    weight: 0,
    weight_unit: 'kg',
    inventory_item_id: XXXXX,
    inventory_quantity: 0,
    old_inventory_quantity: 0,
    requires_shipping: true,
    admin_graphql_api_id: 'gid://shopify/ProductVariant/XXXXXXX'
  }
]

 

 

 

With one custom variant

 

/*variants:*/ [
  {
    id: XXXX,
    product_id: XXXXXXXXXX,
    title: 'S',
    price: '7.500',
    sku: '',
    position: 1,
    inventory_policy: 'deny',
    compare_at_price: null,
    fulfillment_service: 'manual',
    inventory_management: null,
    option1: 'S',
    option2: null,
    option3: null,
    created_at: '2021-07-25T05:57:54-04:00',
    updated_at: '2021-07-25T05:57:54-04:00',
    taxable: true,
    barcode: '',
    grams: 0,
    image_id: null,
    weight: 0,
    weight_unit: 'kg',
    inventory_item_id: XXX,
    inventory_quantity: 0,
    old_inventory_quantity: 0,
    requires_shipping: true,
    admin_graphql_api_id: 'gid://shopify/ProductVariant/XXXX'
  }
]

 

 

 

How to reliably tell if the product has one custom variant

On the resource selector, I used the prop `hasOnlyDefaultVariant` but in the webhook body, it isn't present at all

Thank you

0 Likes
tunisoft
Excursionist
13 3 0

This is an accepted solution.

I ended up making a graphql request like so

const _query = `
  query getProduct($id: ID!) {
    product(id: $id) {
      hasOnlyDefaultVariant
    }
  }
`;
0 Likes