php 实现sku
PHP实现SKU功能
SKU(Stock Keeping Unit)是库存管理中的关键概念,用于唯一标识商品的不同属性组合。以下是PHP实现SKU功能的几种常见方法:
数据库设计
创建商品表和SKU关联表是基础。商品表存储基本信息,SKU表存储具体属性组合和库存信息。
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` text,
PRIMARY KEY (`id`)
);
CREATE TABLE `product_skus` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`sku_code` varchar(50) NOT NULL,
`price` decimal(10,2) NOT NULL,
`stock` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `sku_code` (`sku_code`),
KEY `product_id` (`product_id`)
);
CREATE TABLE `product_attributes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `product_id` (`product_id`)
);
CREATE TABLE `product_attribute_values` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`attribute_id` int(11) NOT NULL,
`value` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `attribute_id` (`attribute_id`)
);
CREATE TABLE `product_sku_attributes` (
`sku_id` int(11) NOT NULL,
`attribute_value_id` int(11) NOT NULL,
PRIMARY KEY (`sku_id`,`attribute_value_id`)
);
生成SKU编码
SKU编码通常由商品ID和属性组合生成,确保唯一性。

function generateSkuCode($productId, $attributeValues) {
$attributePart = '';
foreach ($attributeValues as $valueId) {
$attributePart .= substr(md5($valueId), 0, 2);
}
return 'SKU-' . $productId . '-' . $attributePart;
}
处理多属性组合
使用递归或迭代方法生成所有可能的属性组合。
function generateAllCombinations($attributes) {
$result = array(array());
foreach ($attributes as $attribute) {
$temp = array();
foreach ($result as $item) {
foreach ($attribute['values'] as $value) {
$newItem = $item;
$newItem[] = array(
'attribute_id' => $attribute['id'],
'value_id' => $value['id']
);
$temp[] = $newItem;
}
}
$result = $temp;
}
return $result;
}
前端交互实现
使用JavaScript动态更新SKU选择界面,根据用户选择的属性组合显示对应的SKU信息。

// 示例jQuery代码
$('.attribute-select').change(function() {
var selectedAttributes = {};
$('.attribute-select').each(function() {
var attrId = $(this).data('attribute-id');
var valId = $(this).val();
if (valId) {
selectedAttributes[attrId] = valId;
}
});
// AJAX请求获取匹配的SKU
$.post('/get-sku', {attributes: selectedAttributes}, function(response) {
if (response.sku) {
$('#price').text(response.price);
$('#stock').text(response.stock);
$('#sku-code').text(response.sku);
} else {
// 处理无匹配SKU情况
}
});
});
后端处理逻辑
PHP接收前端属性选择,查询数据库返回匹配的SKU信息。
// 获取匹配的SKU
public function getMatchingSku($productId, $selectedAttributes) {
$query = "SELECT ps.* FROM product_skus ps
JOIN product_sku_attributes psa ON ps.id = psa.sku_id
WHERE ps.product_id = :productId
GROUP BY ps.id
HAVING SUM(CASE WHEN psa.attribute_value_id IN (:attributeValues) THEN 1 ELSE 0 END) = :attrCount
AND COUNT(*) = :attrCount";
$attributeValues = array_values($selectedAttributes);
$attrCount = count($selectedAttributes);
$stmt = $this->pdo->prepare($query);
$stmt->execute([
':productId' => $productId,
':attributeValues' => implode(',', $attributeValues),
':attrCount' => $attrCount
]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
库存管理
实现库存增减功能,确保数据一致性。
public function updateStock($skuId, $quantity, $operation = 'increase') {
$operator = $operation === 'increase' ? '+' : '-';
$query = "UPDATE product_skus SET stock = stock {$operator} :quantity
WHERE id = :skuId AND (stock >= :quantity OR :operator = '+')";
$stmt = $this->pdo->prepare($query);
$params = [
':quantity' => abs($quantity),
':skuId' => $skuId,
':operator' => $operator
];
return $stmt->execute($params);
}
性能优化
对于属性复杂的商品,考虑使用缓存机制存储SKU信息。
public function getSkuWithCache($skuId) {
$cacheKey = "sku_{$skuId}";
if ($this->cache->has($cacheKey)) {
return $this->cache->get($cacheKey);
}
$sku = $this->getSkuFromDb($skuId);
$this->cache->set($cacheKey, $sku, 3600); // 缓存1小时
return $sku;
}
以上方法提供了PHP实现SKU功能的核心思路,可根据实际业务需求进行调整和扩展。






