fix: 规格做法生成SKU时叠加附加价
This commit is contained in:
@@ -25,6 +25,31 @@ export function createProductDetailSkuActions(
|
||||
) {
|
||||
const { form, skuBatch, specTemplateOptions } = options;
|
||||
|
||||
function normalizeMoney(value: number) {
|
||||
return Number(Number(value).toFixed(2));
|
||||
}
|
||||
|
||||
function resolveSpecOptionExtraPrice(templateId: string, optionId: string) {
|
||||
const template = specTemplateOptions.value.find(
|
||||
(item) => item.id === templateId,
|
||||
);
|
||||
if (!template) return 0;
|
||||
const option = template.values.find((item) => item.id === optionId);
|
||||
if (!option) return 0;
|
||||
const extraPrice = Number(option.extraPrice || 0);
|
||||
return Number.isFinite(extraPrice) ? extraPrice : 0;
|
||||
}
|
||||
|
||||
function resolveSkuAttrsExtraPrice(
|
||||
attrs: ProductDetailSkuRowState['attributes'],
|
||||
) {
|
||||
return attrs.reduce(
|
||||
(sum, item) =>
|
||||
sum + resolveSpecOptionExtraPrice(item.templateId, item.optionId),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
function getTemplateName(templateId: string) {
|
||||
return (
|
||||
specTemplateOptions.value.find((item) => item.id === templateId)?.name ||
|
||||
@@ -97,15 +122,23 @@ export function createProductDetailSkuActions(
|
||||
const key = buildSkuKey(attrs);
|
||||
const cached = previousMap.get(key);
|
||||
const skuIndex = index + 1;
|
||||
const totalExtraPrice = resolveSkuAttrsExtraPrice(attrs);
|
||||
const defaultPrice = normalizeMoney(
|
||||
Math.max(0, Number(form.price || 0) + totalExtraPrice),
|
||||
);
|
||||
const defaultOriginalPrice =
|
||||
form.originalPrice !== null &&
|
||||
form.originalPrice !== undefined &&
|
||||
form.originalPrice > 0
|
||||
? normalizeMoney(
|
||||
Math.max(0, Number(form.originalPrice) + totalExtraPrice),
|
||||
)
|
||||
: null;
|
||||
return {
|
||||
id: cached?.id || '',
|
||||
skuCode: cached?.skuCode || buildLocalSkuCode(skuIndex),
|
||||
price: cached?.price ?? Number(form.price || 0),
|
||||
originalPrice:
|
||||
cached?.originalPrice ??
|
||||
(form.originalPrice && form.originalPrice > 0
|
||||
? form.originalPrice
|
||||
: null),
|
||||
price: cached?.price ?? defaultPrice,
|
||||
originalPrice: cached?.originalPrice ?? defaultOriginalPrice,
|
||||
stock:
|
||||
cached?.stock ?? Math.max(0, Math.floor(Number(form.stock || 0))),
|
||||
isEnabled: cached?.isEnabled ?? true,
|
||||
|
||||
Reference in New Issue
Block a user