Reputation: 9491
Old versions of the Shopify GraphQL productCreate
mutation seemingly offered a variants
field where you could set the price of the product you were creating.
The latest version as of writing this (2024-10) does not seem to have this option.
Is there a way to create a basic product with only one variant (or no variants!) and set a price in a single operation?
And if it must be done in two operations? What is the correct second mutation?
productVariantsBulkCreate
seems like a likely option, but the "bulk" word in the name makes me think there is probably a simpler mutation. But that seems to be the only mutation that lets you create a price.Upvotes: -1
Views: 146
Reputation: 1
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.
Upvotes: -1
Reputation: 560
@CleverPatrick , We need to follow these steps:
First, create a product for ID, then add a variant with price, though productVariantsBulkCreate is preferred even for single variants.
Upvotes: 1