From 1cf63774e75d7051351f16d2773718841eeebfde Mon Sep 17 00:00:00 2001 From: Agata Firlejczyk Date: Wed, 27 Nov 2019 22:07:30 +0100 Subject: [PATCH] taxcal - fixes after refactoring. Check if product has special_price and final_price fields. Fix for applying final_price as price. --- src/lib/taxcalc.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/lib/taxcalc.js b/src/lib/taxcalc.js index 5185a3eb..e3354b77 100644 --- a/src/lib/taxcalc.js +++ b/src/lib/taxcalc.js @@ -72,6 +72,7 @@ export function updateProductPrices ({ product, rate, sourcePriceInclTax = false product.hasOwnProperty('original_final_price') && product.hasOwnProperty('original_special_price') ) + // build objects with original price and tax // for first calculation use `price`, for next one use `original_price` const priceWithTax = createSinglePrice(parseFloat(product.original_price || product.price), rate_factor, sourcePriceInclTax && !hasOriginalPrices) @@ -81,24 +82,30 @@ export function updateProductPrices ({ product, rate, sourcePriceInclTax = false // save original prices if (!hasOriginalPrices) { assignPrice({product, target: 'original_price', ...priceWithTax, deprecatedPriceFieldsSupport}) - - product.original_final_price = finalPriceWithTax.price - product.original_special_price = specialPriceWithTax.price + if (specialPriceWithTax.price) { + product.original_special_price = specialPriceWithTax.price + } + if (finalPriceWithTax.price) { + product.original_final_price = finalPriceWithTax.price + } } // reset previous calculation assignPrice({product, target: 'price', ...priceWithTax, deprecatedPriceFieldsSupport}) - assignPrice({product, target: 'final_price', ...finalPriceWithTax, deprecatedPriceFieldsSupport}) - assignPrice({product, target: 'special_price', ...specialPriceWithTax, deprecatedPriceFieldsSupport}) + if (specialPriceWithTax.price) { + assignPrice({product, target: 'special_price', ...specialPriceWithTax, deprecatedPriceFieldsSupport}) + } + if (finalPriceWithTax.price) { + assignPrice({product, target: 'final_price', ...finalPriceWithTax, deprecatedPriceFieldsSupport}) + } if (product.final_price) { if (product.final_price < product.price) { // compare the prices with the product final price if provided; final prices is used in case of active catalog promo rules for example + assignPrice({product, target: 'price', price: product.final_price, deprecatedPriceFieldsSupport}) if (product.final_price < product.special_price) { // for VS - special_price is any price lowered than regular price (`price`); in Magento there is a separate mechanism for setting the `special_prices` - assignPrice({product, target: 'price', ...specialPriceWithTax, deprecatedPriceFieldsSupport}) // if the `final_price` is lower than the original `special_price` - it means some catalog rules were applied over it + assignPrice({product, target: 'price', price: product.special_price, deprecatedPriceFieldsSupport}) // if the `final_price` is lower than the original `special_price` - it means some catalog rules were applied over it } - assignPrice({product, target: 'special_price', ...finalPriceWithTax, deprecatedPriceFieldsSupport}) - } else { - assignPrice({product, target: 'price', ...finalPriceWithTax, deprecatedPriceFieldsSupport}) + assignPrice({product, target: 'special_price', price: product.final_price, deprecatedPriceFieldsSupport}) } } @@ -107,7 +114,7 @@ export function updateProductPrices ({ product, rate, sourcePriceInclTax = false // out of the dates period assignPrice({product, target: 'special_price', price: 0, tax: 0, deprecatedPriceFieldsSupport}) } else { - assignPrice({product, target: 'price', ...specialPriceWithTax, deprecatedPriceFieldsSupport}) + assignPrice({product, target: 'price', price: product.special_price, deprecatedPriceFieldsSupport}) } } else { // the same price as original; it's not a promotion