create-brainerce-store 1.27.5 → 1.27.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -157,20 +157,35 @@ export function ProductClientSection({ product: initialProduct }: ProductClientS
157
157
  // Price info - use variant price if selected, else product price
158
158
  const priceInfo = useMemo(() => {
159
159
  if (selectedVariant?.price) {
160
+ const variantBase = parseFloat(selectedVariant.price);
161
+ const variantSale = selectedVariant.salePrice ? parseFloat(selectedVariant.salePrice) : null;
162
+ const variantEffective =
163
+ variantSale != null && variantSale < variantBase ? variantSale : variantBase;
164
+
165
+ // Overlay any product-level discount rule onto the variant price using the rule's ratio
166
+ if (product.discount) {
167
+ const ruleOriginal = parseFloat(product.discount.originalPrice) || 0;
168
+ const ruleDiscounted = parseFloat(product.discount.discountedPrice) || 0;
169
+ const ratio = ruleOriginal > 0 ? ruleDiscounted / ruleOriginal : 1;
170
+ const discounted = variantEffective * ratio;
171
+ const amount = Math.max(0, variantEffective - discounted);
172
+ return {
173
+ price: discounted,
174
+ originalPrice: variantEffective,
175
+ isOnSale: discounted < variantEffective,
176
+ discountAmount: amount,
177
+ discountPercent:
178
+ variantEffective > 0 ? Math.round((amount / variantEffective) * 100) : 0,
179
+ };
180
+ }
181
+
160
182
  return {
161
- price: parseFloat(selectedVariant.salePrice || selectedVariant.price),
162
- originalPrice: parseFloat(selectedVariant.price),
163
- isOnSale:
164
- selectedVariant.salePrice != null &&
165
- parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price),
183
+ price: variantEffective,
184
+ originalPrice: variantBase,
185
+ isOnSale: variantEffective < variantBase,
166
186
  discountPercent:
167
- selectedVariant.salePrice != null &&
168
- parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price)
169
- ? Math.round(
170
- ((parseFloat(selectedVariant.price) - parseFloat(selectedVariant.salePrice)) /
171
- parseFloat(selectedVariant.price)) *
172
- 100
173
- )
187
+ variantEffective < variantBase && variantBase > 0
188
+ ? Math.round(((variantBase - variantEffective) / variantBase) * 100)
174
189
  : 0,
175
190
  };
176
191
  }