Ecommerce Shopify WordPress Discussion

Getting "The Variant 'Red' Already Exists." When Trying To Add A New Option To An Existing Variant using the Shopify REST api & nodeJS client

I'm attempting to update an existing product and variant through the REST API with a new option. I'm using the latest nodeJS shopify client to do so. To create my product and variants my code looks like: const product = new shopify.rest.Product({ session: session }); product.title = 'A very fake product'; product.vendor = 'Fake Vendor'; product.product_type = 'fake type'; product.body_html = '<strong>Good Fake!</strong>'; product.status = 'draft'; product.options = [{ name: 'Colour', values: ['Red', 'Green'], position: 1 }]; product.variants = [ { title: '_Red', price: '4.99', sku: 'fake-product-1_red', option1: 'Red', fulfillment_service: 'manual', inventory_management: null }, { title: '_Green', price: '4.99', sku: 'fake-product-1_green', option1: 'Green', fulfillment_service: 'manual', inventory_management: null } ]; await product.save({ update: true }); This works fine and my product with it's variants show up in my development store exactly as I expect. Then I try to send a new request to update the existing product and variants by adding a new size option. const product = new shopify.rest.Product({ session: session }); product.id = '8924178776345'; product.options = [ { name: 'Colour', values: ['Red', 'Green'], position: 1 }, { name: 'Size', values: ['Small', 'Large'], position: 2 } ]; product.variants = [ { id: '47396503945497', price: '4.99', sku: 'fake-product-1_Red_Small', fulfillment_service: 'manual', inventory_management: null, option1: 'Red', option2: 'Small' }, { id: '47396503978265', price: '4.99', sku: 'fake-product-1_green_small', option1: 'Green', option2: 'Small' }, { price: '4.99', sku: 'fake-product-1_red_large', option1: 'Red', option2: 'Large' }, { price: '4.99', sku: 'fake-product-1_green_large', option1: 'Green', option2: 'Large' } ]; await product.save({ update: true }); So I'm trying to update the first two variants by setting their id which I'm storing between requests but I'm getting the following error when I try to save the product: (422 Unprocessable Entity) from Shopify: { "base": [ "The variant 'Red' already exists." ] I'm guessing the only way to make this work is to either delete all the variants and options in one message, and send a subsequent http request that creates the options and variants from scratch, but then any outstanding orders/order history would be lost in the shopify store so that's not acceptable. I'm going to try setting the values of option1, option2 & option3 to placeholder values if they're not being used and see if that allows updating variant options while retaining the variant id and I'll update this post with how that goes. I've had a read through all the similar questions on here but can't find any with the same error "The Variant already exists". EDIT: I went and actually ran the above code instead of how my app was building the request object from the database and the simple example above worked fine, so the problem is with my app code and not the API. I'll leave the message up for now as an example and update with any further details. EDIT2The problem in my app code was down to not setting the product.options on the second request. This was due to simple bug/oversight. I'll leave this up here as a reference in case anyone else runs into the same mistake.
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.