Create multiple variants with same option

Solved
pat0x00
New Member
4 0 0

I'm trying to build an app that is creating product variants based on some calculation of the current cart. So the basic functionality is as follows:

1. customer adds one or multiple items to cart

2. app calculates a price based on those items

3. app creates a product variant (of a base product that is created on app install) with the price that is calculated

4. newly created product variant is added to cart

The problem here is that the calculated prices could be the same which leads to the variant actually being the same as an existing one and thus I can't create this variant. Is there a way to create multiple variants with the exact same options?

0 Likes
jchristie
Excursionist
13 2 4

This is an accepted solution.

Hey,

There are a couple of solutions I can think of, both of which require the Storefront API if I'm not mistaken.

1. To stick with you current design, you could store a map in your apps backend to map from the products properties, e.g. price to a product variant id. For example, say you want to add a variant with a price of $45, you first check the map to see if a variant with the price of $45 exists, if it does, you just retrieve that variant and use that instead. And if not, you create the variant and save the ID in the map. To find out what customers have in their cart you'd need the Checkout endpoint, part of the Storefront API.

2. I'm not exactly sure of your apps use-case, but from your description it appears you could simply apply a discount to the checkout (again using the Storefront API) or actually update the CheckoutLineItems price in the checkout itself. Meaning you sell the same variant, but change the price within the Checkout object itself. If the newly created product variant needs to be added to the card, you could do a mixture of the the two and simply add the base product to the cart and set the price of the CheckoutLineItem of the base product within the Checkout.

Hope this helped a bit or at least gave you some ideas.

James

pat0x00
New Member
4 0 0

Hey,

 

thanks for your answer. The first solution would probably be the best fit for our problem. 

0 Likes