php实现商品规格
商品规格数据库设计
商品规格通常需要设计多张关联表来存储规格信息。常见的表结构包括:
- 商品表(products):存储商品基本信息
- 规格组表(spec_groups):存储规格分类(如颜色、尺寸等)
- 规格值表(spec_items):存储具体规格值(如红色、XL等)
- 商品规格关联表(product_specs):关联商品与规格
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `spec_groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `spec_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` int(11) NOT NULL,
`value` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `product_specs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`spec_item_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
规格数据操作
添加规格组和规格值:
// 添加规格组
function addSpecGroup($name) {
$sql = "INSERT INTO spec_groups (name) VALUES (?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$name]);
return $pdo->lastInsertId();
}
// 添加规格值
function addSpecItem($groupId, $value) {
$sql = "INSERT INTO spec_items (group_id, value) VALUES (?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$groupId, $value]);
return $pdo->lastInsertId();
}
商品与规格关联
为商品添加规格:

function addProductSpec($productId, $specItemId) {
$sql = "INSERT INTO product_specs (product_id, spec_item_id) VALUES (?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$productId, $specItemId]);
return $pdo->lastInsertId();
}
获取商品规格
获取商品所有规格信息:
function getProductSpecs($productId) {
$sql = "SELECT sg.name as group_name, si.value as spec_value
FROM product_specs ps
JOIN spec_items si ON ps.spec_item_id = si.id
JOIN spec_groups sg ON si.group_id = sg.id
WHERE ps.product_id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$productId]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
规格组合展示
前端展示规格组合:

function getProductSpecGroups($productId) {
$specs = getProductSpecs($productId);
$groups = [];
foreach($specs as $spec) {
$groupName = $spec['group_name'];
if(!isset($groups[$groupName])) {
$groups[$groupName] = [];
}
$groups[$groupName][] = $spec['spec_value'];
}
return $groups;
}
规格SKU生成
生成商品SKU组合:
function generateProductSKUs($productId) {
$specGroups = getProductSpecGroups($productId);
$result = [];
// 使用递归生成所有规格组合
function generateCombinations($groups, $current = [], $groupNames = []) {
if(empty($groups)) {
return [array_combine($groupNames, $current)];
}
$groupName = key($groups);
$items = array_shift($groups);
$combinations = [];
foreach($items as $item) {
$newCurrent = array_merge($current, [$item]);
$combinations = array_merge(
$combinations,
generateCombinations($groups, $newCurrent, array_merge($groupNames, [$groupName]))
);
}
return $combinations;
}
return generateCombinations($specGroups);
}
规格价格处理
不同规格可能有不同价格:
function getSpecPrice($productId, $specValues) {
// 根据规格组合查询价格
// 实现逻辑取决于具体业务需求
}
规格库存管理
跟踪每个规格组合的库存:
CREATE TABLE `product_skus` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`spec_combination` varchar(255) NOT NULL,
`stock` int(11) NOT NULL,
`price` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
);
function updateSkuStock($skuId, $quantity) {
$sql = "UPDATE product_skus SET stock = stock + ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$quantity, $skuId]);
}






