对于产品,有两种模式产品,参看:http://www.fecshop.com/doc/fecshop-guide/instructions/cn-1.0/guide-fecshop_product.html#1
1.淘宝模式产品的总库存(qty字段),是如何计算的
对于淘宝模式的产品(参看上面的链接往下拖),会看到淘宝模式的产品
也就是产品存在子项,也就是 custom option ,产品的每个custom option 子项,都有独立的sku和库存个数, 因此,产品保存的时候,产品的总库存是各个子项的库存的总和,代码参看:https://github.com/fecshop/yii2_fecshop/blob/master/services/product/ProductMongodb.php#L324
/**
* 如果 $one['custom_option'] 不为空,则计算出来库存总数,填写到qty
*/
if (is_array($one['custom_option']) && !empty($one['custom_option'])) {
$custom_option_qty = 0;
foreach ($one['custom_option'] as $co_one) {
$custom_option_qty += $co_one['qty'];
}
$model->qty = $custom_option_qty;
}
因此,对于淘宝模式的产品,在编辑部分填写的qty会被custom option部分的库存总和覆盖掉
2.库存的扣除
在代码:https://github.com/fecshop/yii2_fecshop/blob/master/services/product/Stock.php#L239 ,可以看到对于淘宝模式的产品,总库存和custom option的库存都是扣除的
if ($product_id && $sale_qty) {
// 应对高并发库存超卖的控制,扣除库存的时候,加上qty个数的查询,不满足查询条件则不扣除库存
$updateColumns = $this->_flatQtyModel->updateAllCounters(
['qty' => 0 - $sale_qty],
['and', ['product_id' => $product_id], ['>=', 'qty', $sale_qty]]
);
if (empty($updateColumns)) {// 上面更新sql返回的更新行数如果为0,则说明更新失败,产品不存在,或者产品库存不够
Yii::$service->helper->errors->add('product: [ {product_name} ] is stock out', ['product_name' => $product_name]);
return false;
}
// 对于custom option(淘宝模式)的库存扣除
if ($custom_option_sku) {
$updateColumns = $this->_COQtyModel->updateAllCounters(
['qty' => 0 - $sale_qty],
[
'and',
[
'custom_option_sku' => $custom_option_sku,
'product_id' => $product_id
],
['>=','qty',$sale_qty]
]
);
if (empty($updateColumns)) {// 上面更新sql返回的更新行数如果为0,则说明更新失败,产品不存在,或者产品库存不够
Yii::$service->helper->errors->add('product: [ {product_name} ] is stock out', ['product_name' => $product_name]);
return false;
}
}
3.库存的返还
对于订单取消的产品,后台脚本会处理返还产品的库存,参看: https://github.com/fecshop/yii2_fecshop/blob/master/services/product/Stock.php#L287
可以看到,淘宝模式的主库存和custom option的库存都会返还。