Blog Details
Pabon Saha
30 Oct 2024
6 min read
Managing product variants is essential for e-commerce platforms, especially in multi-vendor setups where diverse products, attributes, and variants can quickly become complex. In this post, we’ll explore a structured approach to organizing and storing product variants in a way that’s scalable and easy to manage.
The first step is to create a central Attributes table to hold general attributes that apply to products across the platform. Attributes like size, color, and material give customers flexibility and allow you to customize each product.
Attributes Table Example:
Attribute ID | Attribute Name |
---|---|
1 | Size |
2 | Color |
3 | Material |
Each row here represents an attribute category, which can later be linked to individual products as needed.
Once you’ve set up the Attributes table, it’s time to add an Attributes Values table. This table stores specific values for each attribute. For instance, if "Color" is an attribute, then "Red," "Blue," and "Green" would be values under that attribute. By structuring it this way, we keep things organized and flexible.
Attributes Values Table Example:
Attribute Value ID | Attribute ID | Name | Value |
---|---|---|---|
1 | 2 | Red | #00ff00 |
2 | 2 | Blue | #00ff33 |
3 | 1 | M | |
4 | 1 | L | |
5 | 3 | Cotton | |
6 | 3 | Wool |
This setup allows each product to have multiple possible values for each attribute, and it keeps data centralized and easy to manage.
With the attributes structured, the next step is to set up the Product table, where each row represents a unique product in the catalog. This table holds general product information, like the name, description, and base price, which will be consistent across all variants.
Product Table Example:
The Product table is intentionally broad, allowing it to represent various types of products.
Now for the core piece of the puzzle: the Product Variant table. Each row here represents a unique variant of a product, combining specific attribute values like color, size, or material. This setup enables detailed management and storage of each product variant.
Product Variant Table Example:
Variant ID | Product ID | SKU | Variant | Quantiy | Price |
---|---|---|---|---|---|
1 | 1 | sku258 | Red-Wool | 10 | $10.00 |
2 | 1 | sku912 | Blue-M | 25 | $11.00 |
3 | 2 | sku211 | Denim-Red-S | 52 | $42.00 |
The Product Variant table links each variant back to the Product table, making it easy to retrieve all variants of a given product and keeping things manageable even with a large product catalog.
Modern SQL databases now offer support for JSON data types, which can be a game-changer for efficiently handling product variants. By storing variant data in JSON format, we gain flexibility, scalability, and ease of retrieval, which is especially useful in an e-commerce setting where a single product might have multiple variants.
With a JSON-compatible SQL database, we can store each product’s variants in a variants
column as a JSON array. This structure captures each attribute (such as color, size, and material) and its values in a structured format, allowing us to:
In this structure, each product’s variant attributes are stored in a JSON format that allows for flexible and intuitive retrieval.
In this database structure, each variant is stored as a single row in a Product Variants table, with a few key fields:
product_id
: Links each variant to a main product.variants
: A concatenated string representing specific attributes, such as size, color, and material (e.g., s-red-wool
).price
: The price of this particular variant.quantity
: The available stock for this variant.Here’s an example of the Product Variants table layout:
ID | Product ID | Variants | Price | Quantity |
---|---|---|---|---|
1 | 5 | s-red-wool | $10 | 25 |
2 | 5 | m-blue-cotton | $12 | 15 |
3 | 5 | l-green-wool | $13 | 10 |
Each row represents a unique combination of size, color, and material for a product with product_id = 5
. By using a single variants
column for attributes, we keep our table straightforward and easy to manage.
To display variant options for a specific product, the application can retrieve the variants
JSON data and send it to the frontend. The frontend will receive the data organized by attributes:
This JSON-based setup means that each attribute can be displayed in sequence on the frontend, allowing customers to make a specific selection, such as "Red-M-Wool."
The frontend can send a selected variant combination to the server in a structured format, such as Red-M-Wool
. Here’s how this works in practice:
SELECT price, quantity
FROM product_variants
WHERE product_id = 5
AND variants = 'red-m-wool';
Here’s how you can manage, store, and retrieve product variants in your e-commerce platform with the given database design.
This structure is simple and effective for retrieving specific variants quickly. Benefits include:
variants
string, querying specific variants is straightforward.
Storing product variant data in JSON format within an SQL database simplifies the process of managing complex attribute combinations. This approach is ideal for e-commerce platforms with extensive product catalogs, allowing you to store, retrieve, and display variant data in an intuitive way. JSON’s flexibility and support in SQL databases offer an efficient, scalable solution that can grow with your business.
Don’t worry, we don’t spam!