From 568d6e42bdf970211b93e31ff1b2f95f4d6468e5 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Tue, 28 Oct 2025 20:56:55 +0700 Subject: product desktop variant voucher fix --- .../components/Product/ProductDesktopVariant.jsx | 73 ++++++++++++++++------ 1 file changed, 55 insertions(+), 18 deletions(-) (limited to 'src') 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); -- cgit v1.2.3