Product creation using Graphql api

Product creation using Graphql api

pal3kuno
Tourist
6 0 1

Hello,

 

I want to create a product using the graphql api (ProductCreate endpoint) and I can't find on how to add data for the following fields
- price

- track quantity (checkbox)

- quantity locations

- continue selling when out of stock

- this product has sku or barcode

- sku

- barcode

 

I have attached the screenshot https://imgur.com/a/IcG0038. pls help thank you

Replies 2 (2)

tirthmehta10
Tourist
11 0 2

First, create a unique ID for the product using its title, status, and tags. Then, use this ID to push updates for price, SKU, and other details. Note: Inventory updates require a separate mutation and will not happen in the productVariantsBulkCreateMutation.

 

Create the Product: The first operation in your code is creating the product in Shopify. You use the productCreate mutation to create the product with the required details such as the title, vendor, and status (which is set to "DRAFT" in your example).

const productCreateMutation = gql`
mutation productCreate($product: ProductCreateInput!) { productCreate(product: $product) { product { id title status } userErrors { field message } } } `; // Example product input const productInput = { title: product.Description || "Untitled Product", vendor: product.SupplierCode || "Unknown Vendor", status: "DRAFT", // Product is created in draft mode tags: ["importLStoSHOP"], }; const productCreateResponse = await client.request(productCreateMutation, { product: productInput });

Above step creates the product but does not include any price or variant details.

 

Now, Create the Variant and Set the Price: Once the product is created, you can add a variant to it using the productVariantsBulkCreate mutation. This is where you set the price, SKU, barcode, and other variant-specific information.

const productVariantsBulkCreateMutation = gql`
  mutation productVariantsBulkCreate($productId: ID!, $strategy: ProductVariantsBulkCreateStrategy, $variants: [ProductVariantsBulkInput!]!) {
    productVariantsBulkCreate(productId: $productId, strategy: $strategy, variants: $variants) {
      product { id }
      productVariants { id inventoryItem { id } }
      userErrors { field message }
    }
  }
`;

// Example variant data
const variants = [{
  inventoryItem: { sku: product.PartNumber || "UNKNOWN-SKU" },
  price: product.CurrentActivePrice || 0.0, // Set the price here
  barcode: product.UPC || null,
}];

const variantsResponse = await client.request(productVariantsBulkCreateMutation, {
  productId,
  strategy: "REMOVE_STANDALONE_VARIANT",
  variants,
});

Above step creates the variant for the product and includes the price for that variant. 

TDM
pal3kuno
Tourist
6 0 1

Hello @tirthmehta10,

 

Thank you for your reply, however the new payload for creating product for the api version 2025-01 requires productOptions. kindly refer to this example https://shopify.dev/docs/api/admin-graphql/latest/mutations/productCreate . Thank you so much