39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
import { updateProductPrice } from '@assets/js/wpj.product.helpers.js';
|
|
|
|
export function reloadQuantityDiscounts(variationId) {
|
|
const $allQuantityDiscounts = $('[data-quantity-discounts="quantityDiscounts"]');
|
|
|
|
if ($allQuantityDiscounts.length > 0) {
|
|
const reloadUrl = window.location.pathname + '?id_variation=' + variationId;
|
|
wpj.domUtils.reloadPartsFromUrl(reloadUrl, $('[data-reload="quantityDiscounts"]'), null, function() {
|
|
checkQuantityDiscount(
|
|
$('.submit-block')
|
|
.find('[name="No"]')
|
|
.val()
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
export function checkQuantityDiscount(count) {
|
|
const $allQuantityDiscounts = $('[data-quantity-discounts="quantityDiscounts"]');
|
|
count = parseInt(count);
|
|
|
|
if ($allQuantityDiscounts.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
const selectedDiscount = wpj.quantityDiscounts.find(el => {
|
|
return el.piecesFrom <= count && (isNaN(el.piecesTo) || count <= el.piecesTo);
|
|
});
|
|
|
|
if (selectedDiscount) {
|
|
$allQuantityDiscounts.find('.active').removeClass('active');
|
|
$allQuantityDiscounts.find(`[data-quantity-discounts-row="${selectedDiscount.index}"]`).addClass('active');
|
|
updateProductPrice(selectedDiscount.price, selectedDiscount.priceNoVat);
|
|
$('body').trigger('quantityDiscountSelected');
|
|
}
|
|
|
|
/* todo: nemeni se cena pred slevou, sleva... */
|
|
}
|