vsf-utilities
Version:
Utilities functions shared between vsf projects
91 lines • 5.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const isSpecialPriceActive_1 = __importDefault(require("./isSpecialPriceActive"));
const createSinglePrice_1 = __importDefault(require("./createSinglePrice"));
const assignPrice_1 = __importDefault(require("./assignPrice"));
function updateProductPrices({ product, rate, sourcePriceInclTax = false, deprecatedPriceFieldsSupport = false, finalPriceInclTax = true }) {
const rate_factor = parseFloat(rate.rate) / 100;
const hasOriginalPrices = (product.original_price !== undefined &&
product.original_final_price !== undefined &&
product.original_special_price !== undefined);
// build objects with original price and tax
// for first calculation use `price`, for next one use `original_price`
const priceWithTax = createSinglePrice_1.default(parseFloat(product.original_price || product.price), rate_factor, sourcePriceInclTax && !hasOriginalPrices);
const finalPriceWithTax = createSinglePrice_1.default(parseFloat(product.original_final_price || product.final_price), rate_factor, finalPriceInclTax && !hasOriginalPrices);
const specialPriceWithTax = createSinglePrice_1.default(parseFloat(product.original_special_price || product.special_price), rate_factor, sourcePriceInclTax && !hasOriginalPrices);
// save original prices
if (!hasOriginalPrices) {
assignPrice_1.default(Object.assign(Object.assign({ product, target: 'original_price' }, priceWithTax), { deprecatedPriceFieldsSupport }));
if (specialPriceWithTax.price) {
product.original_special_price = specialPriceWithTax.price;
}
if (finalPriceWithTax.price) {
product.original_final_price = finalPriceWithTax.price;
}
}
// reset previous calculation
assignPrice_1.default(Object.assign(Object.assign({ product, target: 'price' }, priceWithTax), { deprecatedPriceFieldsSupport }));
if (specialPriceWithTax.price) {
assignPrice_1.default(Object.assign(Object.assign({ product, target: 'special_price' }, specialPriceWithTax), { deprecatedPriceFieldsSupport }));
}
if (finalPriceWithTax.price) {
assignPrice_1.default(Object.assign(Object.assign({ 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_1.default(Object.assign(Object.assign({ product, target: 'price' }, finalPriceWithTax), { deprecatedPriceFieldsSupport }));
if (product.special_price && 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_1.default(Object.assign(Object.assign({ 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_1.default(Object.assign(Object.assign({ product, target: 'special_price' }, finalPriceWithTax), { deprecatedPriceFieldsSupport }));
}
else {
assignPrice_1.default(Object.assign(Object.assign({ product, target: 'price' }, finalPriceWithTax), { deprecatedPriceFieldsSupport }));
}
}
}
if (product.special_price && (product.special_price < product.original_price)) {
if (!isSpecialPriceActive_1.default(product.special_from_date, product.special_to_date)) {
// out of the dates period
assignPrice_1.default({ product, target: 'special_price', price: 0, tax: 0, deprecatedPriceFieldsSupport });
}
else {
assignPrice_1.default(Object.assign(Object.assign({ product, target: 'price' }, specialPriceWithTax), { deprecatedPriceFieldsSupport }));
}
}
else {
// the same price as original; it's not a promotion
assignPrice_1.default({ product, target: 'special_price', price: 0, tax: 0, deprecatedPriceFieldsSupport });
}
if (product.configurable_children) {
for (const configurableChild of product.configurable_children) {
if (configurableChild.custom_attributes) {
for (const opt of configurableChild.custom_attributes) {
configurableChild[opt.attribute_code] = opt.value;
}
}
// update children prices
updateProductPrices({ product: configurableChild, rate, sourcePriceInclTax, deprecatedPriceFieldsSupport, finalPriceInclTax });
if ((configurableChild.price_incl_tax <= product.price_incl_tax) || product.price === 0) { // always show the lowest price
assignPrice_1.default({
product,
target: 'price',
price: configurableChild.price,
tax: configurableChild.price_tax,
deprecatedPriceFieldsSupport
});
assignPrice_1.default({
product,
target: 'special_price',
price: configurableChild.special_price,
tax: configurableChild.special_price_tax,
deprecatedPriceFieldsSupport
});
}
}
}
}
exports.default = updateProductPrices;
//# sourceMappingURL=updateProductPrices.js.map