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.