summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMiqdad <ahmadmiqdad27@gmail.com>2025-10-28 20:56:55 +0700
committerMiqdad <ahmadmiqdad27@gmail.com>2025-10-28 20:56:55 +0700
commit568d6e42bdf970211b93e31ff1b2f95f4d6468e5 (patch)
treee4216d105e06532033b0cedca2d52a00f85c991d /src
parentdf48bc302f38e73cd4ef4b112e92265dc8e5e130 (diff)
<MIqdad> product desktop variant voucher fix
Diffstat (limited to 'src')
-rw-r--r--src/lib/product/components/Product/ProductDesktopVariant.jsx73
1 files changed, 55 insertions, 18 deletions
diff --git a/src/lib/product/components/Product/ProductDesktopVariant.jsx b/src/lib/product/components/Product/ProductDesktopVariant.jsx
index 5e6d7d7f..2d7495ca 100644
--- a/src/lib/product/components/Product/ProductDesktopVariant.jsx
+++ b/src/lib/product/components/Product/ProductDesktopVariant.jsx
@@ -309,35 +309,72 @@ const ProductDesktopVariant = ({
fetchData();
}, [product]);
- let voucherPastiHemat = 0;
- voucherPastiHemat = product?.newVoucherPastiHemat[0]
- ? product?.newVoucherPastiHemat[0]
+ // voucher dari Solr: utamakan newVoucherPastiHemat, fallback voucherPastiHemat
+ let voucherPastiHemat = Array.isArray(product?.newVoucherPastiHemat)
+ ? product.newVoucherPastiHemat[0]
: product?.newVoucherPastiHemat;
+ if (!voucherPastiHemat && Array.isArray(product?.voucherPastiHemat)) {
+ voucherPastiHemat = product.voucherPastiHemat[0];
+ }
+
const hitungDiscountVoucher = () => {
+ const basePrice =
+ Number(product?.price?.priceDiscount ?? 0) ||
+ Number(product?.price?.price ?? 0);
+
+ if (!voucherPastiHemat || !basePrice) {
+ setDiscount(0);
+ return;
+ }
+
+ const type = String(
+ voucherPastiHemat.discountType ?? voucherPastiHemat.discount_type ?? ''
+ ).toLowerCase();
+ const amount = Number(
+ voucherPastiHemat.discountAmount ?? voucherPastiHemat.discount_amount ?? 0
+ );
+ const max = Number(
+ voucherPastiHemat.maxDiscount ?? voucherPastiHemat.max_discount ?? 0
+ );
+ const min = Number(
+ voucherPastiHemat.minPurchase ?? voucherPastiHemat.min_purchase ?? 0
+ );
+
+ if (min > 0 && basePrice < min) {
+ setDiscount(0);
+ return;
+ }
+
let countDiscount = 0;
- if (voucherPastiHemat.discountType === 'percentage') {
- countDiscount =
- product?.lowestPrice.priceDiscount *
- (voucherPastiHemat.discountAmount / 100);
- // console.log('count disc', countDiscount);
- if (
- voucherPastiHemat.maxDiscount > 0 &&
- countDiscount > voucherPastiHemat.maxDiscount
- ) {
- countDiscount = voucherPastiHemat.maxDiscount;
- }
+
+ if (type.startsWith('percent')) {
+ const pct = amount <= 1 ? amount * 100 : amount;
+ countDiscount = Math.floor(basePrice * (pct / 100));
} else {
- countDiscount = voucherPastiHemat.discountAmount;
+ countDiscount = Math.floor(amount || 0);
}
- setDiscount(countDiscount);
- console.log('count disc', countDiscount);
+ if (max > 0 && countDiscount > max) countDiscount = max;
+
+ setDiscount(Math.max(0, countDiscount));
+ console.log('count disc', countDiscount, {
+ basePrice,
+ type,
+ amount,
+ max,
+ min,
+ });
};
useEffect(() => {
hitungDiscountVoucher();
- }, []);
+ }, [
+ product?.price?.priceDiscount,
+ product?.price?.price,
+ product?.newVoucherPastiHemat,
+ product?.voucherPastiHemat,
+ ]);
console.log('product', product);