I am trying to create variants but there is always an error "The variant 'Default Title' already exists," when the variant clearly has a different title.
I have even tried updating the default variant.
I know you can't delete the default variant unless there exists another variant (I tried that and then did some googling lol).
Any help would be appreciated! Thanks.
ShopifyProductVariant(title='13 Frank Beltrame Italian Stiletto with Bison Horn Grips - Green_644110049414', price='28.98', sku=44483968, weight=0.59, weight_unit='lb', id='644110049414_8900', inventory_management=None, taxable=True, barcode=644110049414)
Variant creation payload: {'query': '\n mutation productVariantCreate($input: ProductVariantInput!) {\n productVariantCreate(input: $input) {\n product {\n id\n variants(first: 250) {\n edges {\n node {\n id\n }\n }\n }\n }\n productVariant {\n id\n }\n userErrors {\n field\n message\n }\n }\n }\n ', 'variables': {'input': {'productId': 'gid://shopify/Product/9699571892507', 'title': '13 Frank Beltrame Italian Stiletto with Bison Horn Grips - Green_644110049414', 'price': '28.98', 'barcode': '644110049414', 'taxable': True, 'metafields': [{'namespace': 'custom', 'key': 'sku', 'value': '44483968', 'type': 'single_line_text_field'}, {'namespace': 'custom', 'key': 'inventory_management', 'value': 'None', 'type': 'single_line_text_field'}, {'namespace': 'custom', 'key': 'weight', 'value': '0.59', 'type': 'single_line_text_field'}, {'namespace': 'custom', 'key': 'weight_unit', 'value': 'lb', 'type': 'single_line_text_field'}]}}}
Error creating variant: {'data': {'productVariantCreate': {'product': {'id': 'gid://shopify/Product/9699571892507', 'variants': {'edges': [{'node': {'id': 'gid://shopify/ProductVariant/49480246198555'}}]}}, 'productVariant': None, 'userErrors': [{'field': None, 'message': "The variant 'Default Title' already exists."}]}}, 'extensions': {'cost': {'requestedQueryCost': 23, 'actualQueryCost': 13, 'throttleStatus': {'maximumAvailable': 2000.0, 'currentlyAvailable': 1987, 'restoreRate': 100.0}}}}
```
#CREATE VARIANT MUTATION
def create_variant_with_metafields(product_id, variant😞
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token'
}
# Updated GraphQL mutation for creating a product variant and fetching product with variants
mutation = '''
mutation productVariantCreate($input: ProductVariantInput!) {
productVariantCreate(input: $input) {
product {
id
variants(first: 250) {
edges {
node {
id
}
}
}
}
productVariant {
id
}
userErrors {
field
message
}
}
}
'''
# Prepare the variant input with metafields
variant_input = {
"productId": product_id, # ID of the created product
"title": variant.title,
"price": variant.price,
"barcode": str(variant.barcode) if variant.barcode else None,
"taxable": variant.taxable,
"metafields": [
{
"namespace": "custom",
"key": "sku",
"value": str(variant.sku), # Store SKU as a metafield
"type": "single_line_text_field"
},
{
"namespace": "custom",
"key": "inventory_management",
"value": str(variant.inventory_management),
"type": "single_line_text_field"
},
{
"namespace": "custom",
"key": "weight",
"value": str(variant.weight),
"type": "single_line_text_field"
},
{
"namespace": "custom",
"key": "weight_unit",
"value": variant.weight_unit,
"type": "single_line_text_field"
}
],
}
# Build the request payload
payload = {
'query': mutation,
'variables': {
'input': variant_input
}
}
print("Variant creation payload:", payload) # Debugging line
# Send the GraphQL request to Shopify
response = requests.post(SHOPIFY_GRAPHQL_URL, headers=headers, json=payload)
response_data = response.json()
if response_data.get('data', {}).get('productVariantCreate', {}).get('productVariant'😞
print("Variant with metafields created successfully")
pprint(response_data) # Print response for debugging
else:
print("Error creating variant:", response_data)
```
def update_default_variant(product_id, variant😞
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token':,
}
mutation = '''
mutation productVariantUpdate($input: ProductVariantInput!) {
productVariantUpdate(input: $input) {
productVariant {
id
title
price
}
userErrors {
field
message
}
}
}
'''
variant_input = {
"id": variant.id, # Use existing variant ID
"title": "hus",
"price": "98",
"barcode": str(variant.barcode) if variant.barcode else None,
"taxable": variant.taxable,
"metafields": [
{
"namespace": "custom",
"key": "sku",
"value": str(variant.sku),
"type": "single_line_text_field"
},
{
"namespace": "custom",
"key": "inventory_management",
"value": variant.inventory_management if variant.inventory_management else None,
"type": "single_line_text_field"
},
{
"namespace": "custom",
"key": "weight",
"value": str(variant.weight) if variant.weight else None,
"type": "single_line_text_field"
},
{
"namespace": "custom",
"key": "weight_unit",
"value": variant.weight_unit if variant.weight_unit else None,
"type": "single_line_text_field"
},
]
}
payload = {
'query': mutation,
'variables': {
'input': variant_input
}
}
response = requests.post(SHOPIFY_GRAPHQL_URL, headers=headers, json=payload)
response_data = response.json()
if response_data.get('data', {}).get('productVariantUpdate', {}).get('productVariant'😞
print("Default variant updated successfully")
else:
print("Error updating default variant:", response_data)