Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 358cc0c

Browse files
authored
Merge pull request #172 from DivanteLtd/feature/special-prices-fix
Special price dates checking fix
2 parents 9465bdb + 8d844f8 commit 358cc0c

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/lib/taxcalc.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
function isSpecialPriceActive(fromDate, toDate) {
2+
const now = new Date()
3+
fromDate = new Date(fromDate) || false
4+
toDate = new Date(toDate) || false
5+
6+
if (fromDate && toDate) {
7+
return fromDate < now && toDate > now
8+
}
9+
10+
if (fromDate && !toDate) {
11+
return fromDate < now
12+
}
13+
14+
if (!fromDate && toDate) {
15+
return toDate > now
16+
}
17+
}
18+
119
export function updateProductPrices (product, rate, sourcePriceInclTax = false) {
220
const rateFactor = parseFloat(rate.rate) / 100
321
product.price = parseFloat(product.price)
@@ -22,7 +40,7 @@ export function updateProductPrices (product, rate, sourcePriceInclTax = false)
2240
product.specialPriceInclTax = specialPriceExclTax + product.specialPriceTax
2341

2442
if (product.special_price && (product.special_price < product.price)) {
25-
if ((product.special_to_date && new Date(product.special_to_date) < new Date()) || (product.special_from_date && new Date(product.special_from_date) > new Date())) {
43+
if (!isSpecialPriceActive(product.special_from_date, product.special_to_date)) {
2644
product.special_price = 0 // out of the dates period
2745
} else {
2846
product.originalPrice = priceExclTax
@@ -67,7 +85,7 @@ export function updateProductPrices (product, rate, sourcePriceInclTax = false)
6785
configurableChild.specialPriceInclTax = specialPriceExclTax + configurableChild.specialPriceTax
6886

6987
if (configurableChild.special_price && (configurableChild.special_price < configurableChild.price)) {
70-
if ((configurableChild.special_to_date && new Date(configurableChild.special_to_date) < new Date()) || (configurableChild.special_from_date && new Date(configurableChild.special_from_date) > new Date())) {
88+
if (!isSpecialPriceActive(configurableChild.special_from_date, configurableChild.special_to_date)) {
7189
configurableChild.special_price = 0 // out of the dates period
7290
} else {
7391
configurableChild.originalPrice = priceExclTax

0 commit comments

Comments
 (0)