CryptoTex
CryptoTex

Reputation: 13

How to update product price with graphql on shopify if there is no product variant

I am using a python script with requests to update various properties of a shopify shop using graphql and it works fine.

The problem is I can't figure out how to update a product price when there is no product variant using Graphql

All the documentation refers to mutations on product variants, but most of the products on this shop don't have any variants.

However I read somewhere that products without variants are "default variants" themselves, but i can't find the id for this.

If I pass the product id, or global id, the response is just no id found. I have spent hours on the documentation and I can't find the correct reference. I even asked our banned friend, but wasn't really helpful:)

Here is the working graphql query for updating variants for reference.

query = '''
    mutation productVariantUpdate($input1: ProductVariantInput!) {
              item1: productVariantUpdate(input: $input1) {
                productVariant {
                  id,
                  price
                }
                userErrors {
                  field
                  message
                }
            }
    '''

variables = {
                "input2":{"id":"gid://shopify/ProductVariant/42177699971252","price":15.20}
            }

Upvotes: 0

Views: 1522

Answers (3)

CryptoTex
CryptoTex

Reputation: 13

Ok, The confusing part was, when you have a product the uri has a product id, if you create a variant, there is a variant id in the uri as well. But i didn't realize that the existance of a default variant id as i never came across of this in the gui, so i never tried to run a variant query on a product without multiple variants.

So running:

 {
  product(id: "gid://shopify/Product/123456798"){    
    variants(first:5 ){ 
      nodes {
        id
        price
      }
    }    
  }      
}

Does return the dafault variant id which i can then use for updating.

Upvotes: 0

grovej
grovej

Reputation: 36

Sounds like you're confusing the number of options with variants.

The Product object also has a totalVariants count and hasOnlyDefaultVariant fields

Upvotes: 0

David Lazar
David Lazar

Reputation: 11427

A product has many variants. Between one, and one hundred. End of story. If you have a product ID, you can get the variant IDs. End of story. If you have a variant ID, you can get the variant details. With those, you will find that a variant has an inventory_item_id. Or inventoryItemId. You get the picture? With that, you can get the item! The item has a price. You can use a mutation to set the price. That is how it is done.

If you say you have a product without a variant, that is false. Impossible.

Upvotes: 0

Related Questions