Solved

Getting "The variant 'Red' already exists." when trying to add a new option to an existing variant

BarryWazp
Shopify Partner
4 1 3

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
});

 

 

 

 and when I try to update it:

 

 

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.

 

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.

Accepted Solution (1)

BarryWazp
Shopify Partner
4 1 3

This is an accepted solution.

The problem in my app code was I wasn't setting the new product.options on the second request, this was a simple bug where I'd left an if statement in from an earlier attempt to handle updating products. I'll leave this up as an example in case anyone else runs into the same error. 

View solution in original post

Reply 1 (1)

BarryWazp
Shopify Partner
4 1 3

This is an accepted solution.

The problem in my app code was I wasn't setting the new product.options on the second request, this was a simple bug where I'd left an if statement in from an earlier attempt to handle updating products. I'll leave this up as an example in case anyone else runs into the same error.