Creating product variations with price changes in ASP.NET Core e-commerce app

I’m building an e-commerce site using ASP.NET Core and I’m stuck on how to handle product variations that affect the price. I want to let users create these variations without making a whole new product each time.

I’ve got a basic product model set up, but I’m not sure how to handle attributes that change the price. For example, a shoe might cost different amounts based on size.

Here’s a simplified version of what I’ve got so far:

public class Product
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal BasePrice { get; set; }
    public List<ProductVariation> Variations { get; set; }
}

public class ProductVariation
{
    public string AttributeName { get; set; }
    public string Value { get; set; }
    public decimal PriceAdjustment { get; set; }
}

What’s the best way to let users add these variations when creating a product? Should I use AJAX to add them one by one after the main product is created? Or is there a better approach?

Any tips or ideas would be really helpful. I’ve been up late trying to figure this out and could use some guidance!

Having worked on similar e-commerce projects, I can offer some insights on handling product variations. Instead of creating variations one by one, consider implementing a bulk creation approach. You could use a dynamic form with JavaScript to add multiple variation fields on the product creation page. When submitting, send all the data as a single JSON payload to your backend.

For the controller, you might want to use a custom model binder to handle the complex object. This allows you to bind the incoming JSON directly to your Product and ProductVariation models.

On the database side, consider using a separate table for variations to keep things normalized and easier to query. You can use Entity Framework Core’s relationships to maintain the connection between products and their variations.

Remember to implement proper validation both client-side and server-side to ensure data integrity. Also, think about how you’ll display these variations on the product page - perhaps using a combination of dropdowns or radio buttons depending on the variation type.

hey sailingbreeze, i’ve dealt with similar stuff before. have u considered using a combo of javascript and partial views? u could make a button that adds variation fields dynamically, then use ajax to send it all to the server at once. something like:

$(‘#addVariation’).click(() => {
$.get(‘/Product/AddVariation’, (data) => {
$(‘#variationList’).append(data);
});
});

this way users can add as many variations as they want before submitting. whaddya think?

Hey there SailingBreeze! Your e-commerce project sounds really interesting. I’ve been tinkering with similar stuff myself and I totally get the headache with product variations.

Have you thought about using a dynamic form approach? Instead of creating everything at once, you could have a main product form, then use JavaScript to add variation fields on the fly. Something like:

$('#addVariation').click(function() {
    var newRow = $('<div class="variation-row">');
    newRow.append('<input type="text" name="AttributeName[]" placeholder="Attribute">');
    newRow.append('<input type="text" name="Value[]" placeholder="Value">');
    newRow.append('<input type="number" name="PriceAdjustment[]" placeholder="Price Change">');
    $('#variationContainer').append(newRow);
});

This way, users can add as many variations as they want before submitting. You could even add a little ‘-’ button to remove variations if they make a mistake.

For the backend, maybe you could use a ViewModel that includes both the Product and a list of ProductVariations? Then in your controller, you can create everything in one go.

Just brainstorming here - what do you think? Have you tried anything like this yet? I’m really curious to see how you end up solving this!