Current Location: Home> Latest Articles> Comprehensive Guide to Developing a PHP E-commerce Multi-Variant SKU System: Database Design and Code Implementation

Comprehensive Guide to Developing a PHP E-commerce Multi-Variant SKU System: Database Design and Code Implementation

M66 2025-06-24

PHP Development Guide: Building a Complete Product Multi-Variant SKU System

In the e-commerce domain, a multi-variant SKU system is essential as it helps merchants clearly display various product specifications and attributes, thereby enhancing the shopping experience. This article introduces how to build a robust multi-variant SKU system in PHP, accompanied by practical code examples.

1. Database Design

First, design the database tables to support a multi-variant SKU system. An example structure includes:

  • Products Table (products): Contains product ID, name, price, and other basic information.
  • Specifications Table (specifications): Stores types of specifications such as color, size, etc.
  • Attributes Table (attributes): Stores specific attribute values like red, blue, small, large, etc.
  • SKU Table (skus): Links products, specifications, and attributes, also keeps stock quantity.

2. Data Model Design

Based on the above tables, you can design corresponding PHP class models to clearly express entities and their relationships:

class Product
{
    private $id;
    private $name;
    private $price;
    private $skus;

    // getters and setters
}

class Specification
{
    private $id;
    private $name;

    // getters and setters
}

class Attribute
{
    private $id;
    private $name;

    // getters and setters
}

class SKU
{
    private $id;
    private $product;
    private $specification;
    private $attribute;
    private $stock;

    // getters and setters
}

3. Implementing the Multi-Variant SKU System

In PHP development, frameworks like Laravel can speed up the process. The implementation steps are summarized as:

  1. Create database models for products, specifications, attributes, and SKUs.
  2. Display product details and specification attribute selectors on the product page.
  3. Use AJAX to dynamically fetch SKU information based on user selection, updating price and stock in real-time.
  4. Verify sufficient stock on order submission and update stock accordingly.
  5. Provide backend management features for adding, editing, and deleting products, specifications, attributes, and SKUs.

Below is sample code demonstrating basic methods to fetch product and SKU information, query SKU details, and update stock:

// Fetch product information and SKU list
public function getProduct($product_id)
{
    $product = Product::find($product_id);
    $skus = SKU::where('product_id', $product_id)->get();

    // Return JSON data
    return response()->json([
        'product' => $product,
        'skus' => $skus
    ]);
}

// Get SKU info based on specification and attribute
public function getSKU($specification_id, $attribute_id)
{
    $sku = SKU::where('specification_id', $specification_id)
              ->where('attribute_id', $attribute_id)
              ->first();

    // Return JSON data
    return response()->json([
        'sku' => $sku
    ]);
}

// Update SKU stock quantity
public function updateStock($sku_id, $stock)
{
    $sku = SKU::find($sku_id);
    $sku->stock = $stock;
    $sku->save();

    // Return JSON data
    return response()->json([
        'success' => true
    ]);
}

4. Summary

With reasonable database design and clear data models, combined with dynamic front-end interaction, implementing a product multi-variant SKU management system can significantly improve the user experience and management efficiency of an e-commerce platform. The details can be adjusted based on business needs, but the core concept remains the same. We hope this guide offers valuable reference for PHP developers building multi-variant SKU systems.