259 lines
10 KiB
JavaScript
259 lines
10 KiB
JavaScript
/*
|
|
* 2018 Singleton software
|
|
*
|
|
* @author Singleton software <info@singleton-software.com>
|
|
* @copyright 2018 Singleton software
|
|
*/
|
|
|
|
$(document).ready(function() {
|
|
resizePriceSection();
|
|
setPriceSizeRatio('Catalog');
|
|
removeOriginalPrices();
|
|
|
|
prestashop.on('updateProductList', function (resp) {
|
|
resizePriceSection();
|
|
setPriceSizeRatio('Catalog');
|
|
removeOriginalPrices();
|
|
});
|
|
|
|
prestashop.on('updatedProduct', function (resp) {
|
|
if (parseInt(dualPriceConfigData.show_in_product_detail) == 1) {
|
|
quantity = 1;
|
|
if (typeof getProductParentObject(resp.id_product) !== 'undefined') {
|
|
quantity = parseInt(getProductParentObject(resp.id_product).find(getCorrectPathFromConfiguration('detail_quantity_path')).val());
|
|
if (parseInt(quantity) == 0 || isNaN(quantity) || typeof quantity === 'undefined') {
|
|
quantity = 1;
|
|
}
|
|
}
|
|
getProductCombinationPrices(resp.id_product, resp.id_product_attribute, quantity, false, null);
|
|
}
|
|
});
|
|
if (ltPs1740) {
|
|
$(document).on('change', getCorrectPathFromConfiguration('detail_quantity_path'), function() {
|
|
if ($(this).parents(getCorrectPathFromConfiguration('quickview_parent_path') + ':eq(0)').length > 0) {
|
|
quantity = parseInt($(this).val());
|
|
if (parseInt(quantity) != 0 && !isNaN(quantity) && typeof quantity !== 'undefined') {
|
|
getProductCombinationPrices(0, 0, 0, false, $(this).parents('form:eq(0)').serialize());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
prestashop.on('updateCart', function (event) {
|
|
if (event.reason.linkAction == 'add-to-cart' && parseInt(dualPriceConfigData.show_in_add_to_cart_popup) == 1) {
|
|
var counter = 20;
|
|
var checkIfElementExistRepeatedly = setInterval(function() {
|
|
counter--;
|
|
if (counter === 0 || $(getCorrectPathFromConfiguration('summary_popup_parent_path')).length > 0) {
|
|
if (typeof event.resp !== 'undefined') {
|
|
quantity = event.resp.quantity;
|
|
} else {
|
|
quantity = 1;
|
|
if (typeof getProductParentObject(event.reason.idProduct) !== 'undefined') {
|
|
quantity = parseInt(getProductParentObject(event.reason.idProduct).find(getCorrectPathFromConfiguration('detail_quantity_path')).val());
|
|
}
|
|
}
|
|
if (parseInt(quantity) == 0 || isNaN(quantity) || typeof quantity === 'undefined') {
|
|
quantity = 1;
|
|
}
|
|
getProductCombinationPrices(event.reason.idProduct, event.reason.idProductAttribute, quantity, true, null);
|
|
clearInterval(checkIfElementExistRepeatedly);
|
|
}
|
|
}, 200);
|
|
}
|
|
});
|
|
});
|
|
|
|
function resizePriceSection() {
|
|
$('.dualDisplayPro').each(function() {
|
|
$(this).parents(getCorrectPathFromConfiguration('miniature_root_path') + ':eq(0)').css('margin-bottom', ($(this).height() + 50) + 'px');
|
|
});
|
|
}
|
|
|
|
function setPriceSizeRatio(type) {
|
|
$('.dualDisplayPro.product' + type).each(function() {
|
|
fontSizeOfFirstPrice = parseInt($(this).find('.priceDisplay').children('span:eq(0)').css('font-size'));
|
|
if ($(this).find('.priceDisplay').children('span:eq(1)').length > 0) {
|
|
$(this).find('.priceDisplay').children('span:eq(1)').css('font-size', (fontSizeOfFirstPrice/getSizeRatioFromConfig('standard')) + 'px');
|
|
}
|
|
if ($(this).find('.originalPriceDisplay').length > 0) {
|
|
$(this).find('.originalPriceDisplay').css('font-size', (fontSizeOfFirstPrice/getSizeRatioFromConfig('old')) + 'px');
|
|
}
|
|
});
|
|
}
|
|
|
|
function removeOriginalPrices() {
|
|
$('.dualDisplayPro').each(function() {
|
|
$(this).parents(getCorrectPathFromConfiguration('miniature_root_path') + ':eq(0)').find(getCorrectPathFromConfiguration('miniature_old_price_path')).remove();
|
|
$(this).parents(getCorrectPathFromConfiguration('miniature_root_path') + ':eq(0)').find(getCorrectPathFromConfiguration('miniature_price_path')).remove();
|
|
});
|
|
}
|
|
|
|
function getSizeRatioFromConfig(priceType) {
|
|
if (priceType == 'standard') {
|
|
configSizeRation = dualPriceConfigData.size_ratio_between_prices;
|
|
} else {
|
|
configSizeRation = dualPriceConfigData.size_ratio_old_prices;
|
|
}
|
|
sizeRatio = 1.5;
|
|
if (parseInt(configSizeRation) == 1) {
|
|
sizeRatio = 1;
|
|
} else if (parseInt(configSizeRation) == 2) {
|
|
sizeRatio = 1.5;
|
|
} else if (parseInt(configSizeRation) == 3) {
|
|
sizeRatio = 1.666;
|
|
} else {
|
|
sizeRatio = 1.75;
|
|
}
|
|
return sizeRatio;
|
|
}
|
|
|
|
function getProductCombinationPrices(productID, productAttributeID, quantity, isAddToCartPopup, formParams) {
|
|
if (formParams != null) {
|
|
data = formParams + '&ajax=1&action=getProductCombinationPrice';
|
|
} else {
|
|
data = {
|
|
ajax: true,
|
|
action: 'getProductCombinationPrice',
|
|
productID : productID,
|
|
productAttributeID : productAttributeID,
|
|
quantity: quantity
|
|
}
|
|
}
|
|
$.ajax({
|
|
type: "POST",
|
|
url: productCombinationsControllerLink,
|
|
async: false,
|
|
data : data,
|
|
dataType: 'json',
|
|
beforeSend: function() {
|
|
if (!isAddToCartPopup) {
|
|
displayProgresWheel(true, productID);
|
|
} else {
|
|
displayProgresWheelInAddToCartModal(true);
|
|
}
|
|
},
|
|
success: function(res) {
|
|
if (!isAddToCartPopup) {
|
|
displayProductDetailPrice(res.productID, res.productPriceInDetail);
|
|
} else {
|
|
displayProductPriceInAddToCartModal(res.productID, res.productPriceInDetail);
|
|
}
|
|
}
|
|
})
|
|
.always(function (dataOrjqXHR, textStatus, jqXHRorErrorThrown) {
|
|
if (!isAddToCartPopup) {
|
|
displayProgresWheel(false, productID);
|
|
} else {
|
|
displayProgresWheelInAddToCartModal(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
function displayProductDetailPrice(productID, combinationPrices) {
|
|
$('.productDetailDualDisplayProWrapper[data-id="' + productID + '"].moved').each(function() {
|
|
freshPrices = $(this).html();
|
|
productPricesElement = $(this).parents(($(this).attr('data-type') == 'classic') ? getCorrectPathFromConfiguration('detail_parent_path') + ':eq(0)' : getCorrectPathFromConfiguration('quickview_parent_path') + ':eq(0)').find(getCorrectPathFromConfiguration('detail_price_path'));
|
|
productPricesElement.empty();
|
|
productPricesElement.append(freshPrices);
|
|
if (combinationPrices != null) {
|
|
if (typeof combinationPrices.taxInclPrice !== 'undefined') {
|
|
productPricesElement.find('.dualDisplayPro .priceWithTax .priceWrapper').html(combinationPrices.taxInclPrice);
|
|
}
|
|
if (typeof combinationPrices.taxExclPrice !== 'undefined') {
|
|
productPricesElement.find('.dualDisplayPro .priceWithoutTax .priceWrapper').html(combinationPrices.taxExclPrice);
|
|
}
|
|
if (combinationPrices.hasDiscount) {
|
|
if (typeof combinationPrices.originalTaxInclPrice !== 'undefined') {
|
|
productPricesElement.find('.dualDisplayPro .originalPriceWithTax .priceWrapper').html(combinationPrices.originalTaxInclPrice);
|
|
}
|
|
if (typeof combinationPrices.originalTaxExclPrice !== 'undefined') {
|
|
productPricesElement.find('.dualDisplayPro .originalPriceWithoutTax .priceWrapper').html(combinationPrices.originalTaxExclPrice);
|
|
}
|
|
}
|
|
}
|
|
productPricesElement.show();
|
|
setPriceSizeRatio('Detail');
|
|
});
|
|
}
|
|
|
|
function displayProductPriceInAddToCartModal(productID, combinationPrices) {
|
|
freshPricesModelElement = $('#dualDisplayProWrapperModel');
|
|
productPricesElement = freshPricesModelElement.find('.dualDisplayPro').clone();
|
|
productPricesElement.addClass('addToCartModal');
|
|
productPricesElement.find('.originalPriceDisplay').remove();
|
|
appentToElement = $(getCorrectPathFromConfiguration('summary_popup_product_path'));
|
|
appentToElement.empty();
|
|
productPricesElement.appendTo(appentToElement);
|
|
if (combinationPrices != null) {
|
|
if (typeof combinationPrices.taxInclPrice !== 'undefined') {
|
|
productPricesElement.find('.priceWithTax .priceWrapper').html(combinationPrices.taxInclPrice);
|
|
}
|
|
if (typeof combinationPrices.taxExclPrice !== 'undefined') {
|
|
productPricesElement.find('.priceWithoutTax .priceWrapper').html(combinationPrices.taxExclPrice);
|
|
}
|
|
}
|
|
setPriceSizeRatio('InModal');
|
|
}
|
|
|
|
function moveFreshPrices(productID, isModal) {
|
|
if (isModal) {
|
|
$('.productDetailDualDisplayProWrapper[data-id="' + productID + '"][data-type="modal"].moved').remove();
|
|
}
|
|
originalPriceWrapper = $('.productDetailDualDisplayProWrapper[data-id="' + productID + '"][data-type="' + (isModal ? 'modal' : 'classic') + '"]:not(.moved)');
|
|
appendToElement = $('.productDetailDualDisplayProWrapper[data-id="' + productID + '"][data-type="' + (isModal ? 'modal' : 'classic') + '"]:not(.moved)').parents((isModal ? getCorrectPathFromConfiguration('quickview_parent_path') + ':eq(0)' : getCorrectPathFromConfiguration('detail_parent_path') + ':eq(0)'));
|
|
clonedElement = originalPriceWrapper.clone();
|
|
clonedElement.appendTo(appendToElement);
|
|
clonedElement.addClass('moved');
|
|
originalPriceWrapper.remove();
|
|
}
|
|
|
|
function getProductParentObject(productID) {
|
|
freshPricesElement = $('.productDetailDualDisplayProWrapper[data-id="' + productID + '"].moved:eq(0)');
|
|
freshPricesElement.parents((freshPricesElement.attr('data-type') == 'classic') ? getCorrectPathFromConfiguration('detail_parent_path') + ':eq(0)' : getCorrectPathFromConfiguration('quickview_parent_path') + ':eq(0)').find(getCorrectPathFromConfiguration('detail_price_path'));
|
|
if (freshPricesElement.parents(getCorrectPathFromConfiguration('quickview_parent_path') + ':eq(0)').length == 1) {
|
|
return freshPricesElement.parents(getCorrectPathFromConfiguration('quickview_parent_path') + ':eq(0)');
|
|
}
|
|
if (freshPricesElement.parents(getCorrectPathFromConfiguration('detail_parent_path') + ':eq(0)').length == 1) {
|
|
return freshPricesElement.parents(getCorrectPathFromConfiguration('detail_parent_path') + ':eq(0)');
|
|
}
|
|
}
|
|
|
|
function displayProgresWheel(display, productID) {
|
|
$('.productDetailDualDisplayProWrapper[data-id="' + productID + '"].moved').each(function() {
|
|
productPricesElement = $(this).parents(($(this).attr('data-type') == 'classic') ? getCorrectPathFromConfiguration('detail_parent_path') + ':eq(0)' : getCorrectPathFromConfiguration('quickview_parent_path') + ':eq(0)').find(getCorrectPathFromConfiguration('detail_price_path'));
|
|
if (display) {
|
|
productPricesElement.after(getLoadingSpinnerElement());
|
|
} else {
|
|
productPricesElement.siblings('.loadingSpinner').remove();
|
|
}
|
|
});
|
|
}
|
|
|
|
function displayProgresWheelInAddToCartModal(display) {
|
|
if (display) {
|
|
appentToElement = $(getCorrectPathFromConfiguration('summary_popup_product_path'));
|
|
appentToElement.empty();
|
|
appentToElement.append(getLoadingSpinnerElement());
|
|
} else {
|
|
appentToElement.find('.loadingSpinner').remove();
|
|
}
|
|
}
|
|
|
|
function getCorrectPathFromConfiguration(name) {
|
|
correctValue = '.product-miniature';
|
|
if (dualPriceConfigData[name].length > 0) {
|
|
correctValue = dualPriceConfigData[name];
|
|
} else {
|
|
$(domElementPaths).each(function(key, value){
|
|
if (value['name'] == name) {
|
|
correctValue = value['init_value'];
|
|
}
|
|
});
|
|
}
|
|
return correctValue;
|
|
}
|
|
|
|
function getLoadingSpinnerElement() {
|
|
return "<span class='loadingSpinner'><img src='" + baseDir + "modules/dualpricedisplaypro/views/img/spinner.gif' alt='loading spinner' style='height: 40px;' /></span>";
|
|
} |