Have your say in Community Polls: What was/is your greatest motivation to start your own business?

How to set price , inventory and variants in the productCreate Admin GraphQl API?

How to set price , inventory and variants in the productCreate Admin GraphQl API?

syed85
Shopify Partner
3 0 5
I aim to utilize the most recent version of the Shopify API, specifically the productCreate endpoint, to generate both simple and variant products. However, I've encountered an absence of fields for defining crucial attributes such as price, inventory, or variants in the latest API version.
 
Interestingly, upon inspection, I noticed that the variants field is present in the 2024-01 API version. Given the possibility of these features being deprecated in newer versions, I'm curious about potential substitutes for the variants field in the latest API iteration.
Replies 6 (6)

websensepro
Shopify Partner
1288 145 166

In the latest versions of the Shopify Admin GraphQL API, including versions beyond 2024-01, some fields and methods for creating and managing product variants, pricing, and inventory may have been restructured or moved to different endpoints or mutations. Here’s how you can manage products with variants, pricing, and inventory using the most recent Shopify API.

1. Creating a Product with Variants

In the latest API versions, creating a product and its variants may require a more structured approach, potentially involving multiple steps or different mutations. Below is an approach using the productCreate mutation followed by the productVariantCreate mutation to set up variants:

Step 1: Create the Product

First, create a base product without variants using the productCreate mutation:

 

mutation {
  productCreate(input: {
    title: "Example Product"
    descriptionHtml: "<strong>Good product!</strong>"
    productType: "Clothing"
    vendor: "Example Vendor"
  }) {
    product {
      id
      title
    }
    userErrors {
      field
      message
    }
  }
}

 

 

Need a Shopify developer?
Hire us at WebSensePro
For Shopify Design Changes | Shopify Custom Coding | Custom Modifications
Subscribe to our youtube channel or buy us a Cofee here

Free Shopify Beginner Tutorial
syed85
Shopify Partner
3 0 5

What about the default variant? for example if i want to create a simple product in shopify (without any variants) how can i set the price, inventory for them? is there any API ?

 

NOTE: if i use the productVariantCreate API to set price and inventory for simple products i got below error from graphql api.. it is working fine for variants.. so how can i set price, inventory for simple products (product without options) ? please help..

 

 "userErrors": [
                {
                    "field": null,
                    "message": "The variant 'Default Title' already exists."
                }
            ]

 

 

iffikhan30
Shopify Partner
288 37 53

Hello I am stuck on same situation, they don't give us documentation for how to add price in product 

https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/productCreate

If you resolve this please share you answer

Custom theme and app [remix] expert.

Email: irfan.sarwar.khan30@gmail.com
Chat on WhatsApp

iffikhan30
Shopify Partner
288 37 53

After a Long struggle I found a solution ufff,, I don't know why shopify API it's too hard, make it simple add regular and compare price, any way here is solution 

 

const productCreatePrice = await client.query({
                  data:{
                    query: mutation productVariantsBulkCreate($productId: ID!,$strategy: ProductVariantsBulkCreateStrategy, $variants: [ProductVariantsBulkInput!]!) {
    productVariantsBulkCreate(productId: $productId,strategy: $strategy, variants: $variants) {
      product {
        id
      }
      productVariants {
        id
        metafields(first: 1) {
          edges {
            node {
              namespace
              key
              value
            }
          }
        }
      }
      userErrors {
        field
        message
      }
    }
  },
                    variables:{
                      productId: '<--ADD-RETURN-PRODUCT-ID-HERE-->',
                      strategy: "REMOVE_STANDALONE_VARIANT",
                      variants: [
                        {
                          inventoryItem: {
                            sku: `[IS UP TO YOU]`
                          },
                          inventoryPolicy: "CONTINUE",
                          price: '<--HERE-PRICE-->'
                        }
                      ]
                    }
                  }
                })
Custom theme and app [remix] expert.

Email: irfan.sarwar.khan30@gmail.com
Chat on WhatsApp
aidrian-oconnor
Shopify Partner
1 0 1

Thanks so much for this - I spent way too long trying to work this out before finding your solution.  Working well for me.  Thanks again.

iffikhan30
Shopify Partner
288 37 53

welcome same I spent a lot of time on this. 

Custom theme and app [remix] expert.

Email: irfan.sarwar.khan30@gmail.com
Chat on WhatsApp