All things Shopify and commerce
Hi,
I have the below mutation for productVariantsBulkCreate.
It is pretty much working except for the unitCost where I get the error that unitCost is not defined.
Does anyone know what might be causing this?
Mutation:
mutation CreateProductVariants($productId: ID!, $variantsInput: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variantsInput) { productVariants { id title displayName price selectedOptions { name value } inventoryItem { id unitCost{ amount currencyCode } sku countryCodeOfOrigin harmonizedSystemCode tracked measurement{ weight{ unit value } } requiresShipping } inventoryPolicy inventoryQuantity selectedOptions { name value } } userErrors { field message } } }
variables:
{ price: record.productPrice, // Variant price optionValues: [ // Options for the variant (e.g., Size, Color) { name: size, optionName: "Size" }, { name: color, optionName: "Color" }, ], inventoryItem: { sku: `${record.productName} - ${color} - ${size}`, tracked: trackQuantity, requiresShipping: isPhysicalProduct, harmonizedSystemCode: productData.hsCode, countryCodeOfOrigin: productData.countryOfOriginCode, unitCost:{ amount: cost, currencyCode: currencyCode, }, measurement:{ weight:{ unit: weightUnit, value: weight, }, }, }, inventoryPolicy: continueSelling ? "CONTINUE" : "DENY", inventoryQuantities: [ { availableQuantity: 100, locationId: podifylocationID, // Location ID }, ], };
Thanks in advance!
Bas
It looks like the issue here is that the unitCost field isn’t actually supported in the productVariantsBulkCreate mutation in Shopify’s GraphQL API. That’s why you’re getting the error saying it’s not defined.
Shopify’s API has specific fields that it allows in each mutation, and unitCost isn’t one of the fields allowed in this particular mutation. Cost information, like unitCost, is handled separately in Shopify and can’t be set while creating product variants in bulk.
Remove unitCost from the Input: If you don’t absolutely need to include unitCost in this step, just remove it from the input. Your mutation should work without it. Here’s how your inventoryItem input would look:
{ "inventoryItem": { "sku": `${record.productName} - ${color} - ${size}`, "tracked": trackQuantity, "requiresShipping": isPhysicalProduct, "harmonizedSystemCode": productData.hsCode, "countryCodeOfOrigin": productData.countryOfOriginCode, "measurement": { "weight": { "unit": weightUnit, "value": weight } } } }
This way, the mutation doesn’t try to send a field that Shopify doesn’t recognize.
Set the Cost Separately: If you need to set or update the unitCost, you can do it after the variants are created by using a different mutation. Shopify provides a way to update inventory item details with the inventoryItemUpdate mutation. Here’s an example:
Mutation to Update Unit Cost:
mutation UpdateUnitCost($inventoryItemId: ID!, $unitCost: MoneyInput!) { inventoryItemUpdate(id: $inventoryItemId, cost: $unitCost) { inventoryItem { id cost { amount currencyCode } } userErrors { field message } } }
Example Variables:
{ "inventoryItemId": "gid://shopify/InventoryItem/1234567890", "unitCost": { "amount": "15.00", "currencyCode": "USD" } }
So the workflow would look like this:
Double-Check Shopify’s API Docs: Shopify’s API is pretty strict about what fields can be used where. It’s always a good idea to check the documentation for the mutation you’re using. In this case, productVariantsBulkCreate doesn’t allow unitCost.
The error is happening because unitCost isn’t supported in this mutation. To fix it:
Let me know if you have more questions or need help setting this up! 😊
Thanks for the information!
That is indeed how far I got as well. For now I have left the cost out and then the action is running as expected.
I do wonder if the documentation on Shopify's side is up to date on this.
On the .dev docs page for productVariantsBulkCreate (https://shopify.dev/docs/api/admin-graphql/2024-10/mutations/productvariantsbulkcreate) it does show that cost should be available under variants > inventoryItem > cost
However when I update to this structure:
mutation CreateProductVariants($productId: ID!, $variantsInput: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variantsInput) {
productVariants {
id
title
displayName
price
selectedOptions {
name
value
}
inventoryItem {
id
sku
cost
countryCodeOfOrigin
harmonizedSystemCode
tracked
measurement{
weight{
unit
value
}
}
requiresShipping
}
inventoryPolicy
inventoryQuantity
selectedOptions {
name
value
}
}
userErrors {
field
message
}
}
}
it gives the error response:
"equestError: Field 'cost' doesn't exist on type 'InventoryItem'"
Extensions:
{
"code": "undefinedField",
"fieldName": "cost",
"typeName": "InventoryItem"
}
Options: {
"method": "POST",
"url": "https://podify-pro.myshopify.com/admin/api/2024-10/graphql.json"
}
path: [
"mutation CreateProductVariants",
"productVariantsBulkCreate",
"productVariants",
"inventoryItem",
"cost"
]
responseBody:
{
"errors": [
{
"extensions": {
"code": "undefinedField",
"fieldName": "cost",
"typeName": "InventoryItem"
},
"locations": [
{
"column": 15,
"line": 16
}
],
"message": "Field 'cost' doesn't exist on type 'InventoryItem'",
"path": [
"mutation CreateProductVariants",
"productVariantsBulkCreate",
"productVariants",
"inventoryItem",
"cost"
]
}
]
}
I might be missing a step here, but I think my set up aligns with the documentation.
Only the documentation might not match the actual set up.
If you see any missing steps or errors on my side, please do let me know!
Thanks in advance!
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024