| 1 |
"use strict"; |
| 2 |
|
| 3 |
(function ($) { |
| 4 |
'use strict'; |
| 5 |
|
| 6 |
$(document).on('click', '.merchant-flexible-content-control.volume-discounts-style .layout', function () { |
| 7 |
var $this = $(this), |
| 8 |
$parent = $this.closest('.merchant-flexible-content-control.volume-discounts-style'); |
| 9 |
$parent.find('.layout').removeClass('active'); |
| 10 |
$this.addClass('active'); |
| 11 |
initPreview(); |
| 12 |
}); |
| 13 |
$(document).on('change.merchant keyup', function () { |
| 14 |
initPreview(); |
| 15 |
}); |
| 16 |
function initPreview() { |
| 17 |
var layout = $('.merchant-flexible-content-control.volume-discounts-style').find('.layout.active'), |
| 18 |
titleText = layout.find('.merchant-field-table_title input').val(), |
| 19 |
titleTextColor = layout.find('.merchant-field-title_text_color input').val(), |
| 20 |
titleTextFontSize = layout.find('.merchant-field-title_font_size input').val(), |
| 21 |
titleTextFontWeight = layout.find('.merchant-field-title_font_weight select').val(), |
| 22 |
bgColor = layout.find('.merchant-field-table_item_bg_color input').val(), |
| 23 |
borderColor = layout.find('.merchant-field-table_item_border_color input').val(), |
| 24 |
textColor = layout.find('.merchant-field-table_item_text_color input').val(), |
| 25 |
labelBgColor = layout.find('.merchant-field-table_label_bg_color input').val(), |
| 26 |
labelTextColor = layout.find('.merchant-field-table_label_text_color input').val(), |
| 27 |
discountType = layout.find('.merchant-field-discount_type input:checked').val(), |
| 28 |
discountAmount = +layout.find('.merchant-field-discount input').val(), |
| 29 |
discountAmountField = layout.find('.merchant-field-discount input'), |
| 30 |
saveLabelValue = layout.find('.merchant-field-save_label input').val(), |
| 31 |
buyLabelValue = layout.find('.merchant-field-buy_text input').val(), |
| 32 |
quantityValue = +layout.find('.merchant-field-quantity input').val(), |
| 33 |
cartOfferTitle = layout.find('.merchant-group-field-cart_page .merchant-field-title input').val(), |
| 34 |
checkoutTitle = layout.find('.merchant-group-field-checkout_page .merchant-field-title input').val(), |
| 35 |
checkoutDiscountText = layout.find('.merchant-group-field-checkout_page .merchant-field-discount_text input').val(), |
| 36 |
checkoutButtonText = layout.find('.merchant-group-field-checkout_page .merchant-field-button_text input').val(), |
| 37 |
thankYouTitle = layout.find('.merchant-group-field-thank_you_page .merchant-field-title input').val(), |
| 38 |
thankYouDiscountText = layout.find('.merchant-group-field-thank_you_page .merchant-field-discount_text input').val(), |
| 39 |
thankYouButtonText = layout.find('.merchant-group-field-thank_you_page .merchant-field-button_text input').val(), |
| 40 |
cartBundleButtonText = layout.find('.merchant-group-field-cart_page .merchant-field-button_text input').val(); |
| 41 |
$('.merchant-volume-discounts-title').css({ |
| 42 |
'color': titleTextColor, |
| 43 |
'font-size': titleTextFontSize + 'px', |
| 44 |
'font-weight': titleTextFontWeight |
| 45 |
}).html(titleText); |
| 46 |
$('.merchant-volume-discounts-item').css({ |
| 47 |
'border-color': borderColor, |
| 48 |
'background-color': bgColor, |
| 49 |
'color': textColor |
| 50 |
}); |
| 51 |
var $saveLabelPreview = $('.merchant-volume-discounts-item-label'); |
| 52 |
var $buyLabelPreview = $('.merchant-volume-discounts-buy-label'); |
| 53 |
$saveLabelPreview.find('span:first').css({ |
| 54 |
'background-color': labelBgColor, |
| 55 |
'color': labelTextColor |
| 56 |
}); |
| 57 |
var currency = $saveLabelPreview.closest('.mrc-preview-right-column').attr('data-currency'); |
| 58 |
var discountEach = discountType === 'fixed_discount' ? "".concat(currency).concat(discountAmount) : "".concat(discountAmount, "%"); |
| 59 |
var discountTotal = discountType === 'fixed_discount' ? "".concat(currency).concat(discountAmount * quantityValue) : "".concat(discountAmount, "%"); |
| 60 |
|
| 61 |
// Update Save label content |
| 62 |
saveLabelValue = saveLabelValue.replace(/{amount}|{percent}/g, discountTotal); |
| 63 |
$saveLabelPreview.find('span:first').text(saveLabelValue); |
| 64 |
|
| 65 |
// Update Tier format text content |
| 66 |
buyLabelValue = buyLabelValue.replace(/{discount}|{percent}/g, "<strong>".concat(discountEach, "</strong>")).replace(/{quantity}|{amount}/g, "<strong>".concat(quantityValue, "</strong>")); |
| 67 |
$buyLabelPreview.html(buyLabelValue); |
| 68 |
$('.merchant-cart-preview .my-cart .cart-table .cart-item .product .product-info .upsell-offer').text(cartOfferTitle.replace('{quantity}', '3').replace('{discount}', '20%')); |
| 69 |
$('.merchant-cart-preview .my-cart .cart-table .cart-item .product .product-info .upsell-product .upsell-info .add-to-cart').text(cartBundleButtonText); |
| 70 |
$('.merchant-checkout-preview .offer-title').text(checkoutTitle.replace('{quantity}', '3').replace('{discount}', '10%')); |
| 71 |
$('.merchant-checkout-preview .product-details .product-info p').html(checkoutDiscountText.replace('{discount}', '10%').replace('{product_price}', merchant_volume_discounts.mock_item_price)); |
| 72 |
$('.merchant-checkout-preview .product-details .product-info .add-to-order').text(checkoutButtonText); |
| 73 |
$('.merchant-thank-you-preview .offer-title').text(thankYouTitle.replace('{quantity}', '3').replace('{discount}', '15%').replace('{total_quantity}', '5')); |
| 74 |
$('.merchant-thank-you-preview .product-details .product-info p').html(thankYouDiscountText.replace('{product_price}', merchant_volume_discounts.mock_item_price).replace('{discount}', '10%')); |
| 75 |
$('.merchant-thank-you-preview .product-details .product-info .add-to-order').text(thankYouButtonText); |
| 76 |
if (discountType === 'percentage_discount') { |
| 77 |
discountAmountField.attr('max', 100); |
| 78 |
if (discountAmountField.val() > 100) { |
| 79 |
discountAmountField.val(100); |
| 80 |
} |
| 81 |
} else { |
| 82 |
// remove attribute |
| 83 |
discountAmountField.removeAttr('max'); |
| 84 |
} |
| 85 |
} |
| 86 |
$('.merchant-flexible-content-control.volume-discounts-style .layout:first-child').addClass('active').trigger('click'); |
| 87 |
$(document).on('change', 'input[type="radio"]', function () { |
| 88 |
var value = $(this).val(); |
| 89 |
if (value !== 'percentage_discount' && value !== 'fixed_discount') { |
| 90 |
return; |
| 91 |
} |
| 92 |
var $layout = $(this).closest('.layout'); |
| 93 |
$layout.find('input[type="text"], textarea').each(function () { |
| 94 |
// Define the replacement string based on the radio button value |
| 95 |
var replacement = value === 'percentage_discount' ? '{percent}' : '{amount}'; |
| 96 |
var currentValue = $(this).val(); |
| 97 |
|
| 98 |
/** |
| 99 |
* Previously wrong variable `{amount}` was used for this field. Correct one is `{quantity}`. |
| 100 |
* So fix it as soon as Discount type is changed. |
| 101 |
* Keep it for backward compatibility |
| 102 |
*/ |
| 103 |
if ($(this).attr('name').includes('buy_text')) { |
| 104 |
replacement = '{quantity}'; |
| 105 |
} |
| 106 |
|
| 107 |
// Replace occurrences of {amount} or {percent} with the appropriate replacement |
| 108 |
var newValue = currentValue.replace(/{amount}|{percent}/g, replacement); |
| 109 |
$(this).val(newValue); |
| 110 |
}); |
| 111 |
}); |
| 112 |
function show_single_product_preview() { |
| 113 |
var element = $('.merchant-single-product-preview'); |
| 114 |
element.addClass('show'); |
| 115 |
} |
| 116 |
function hide_single_product_preview() { |
| 117 |
var element = $('.merchant-single-product-preview'); |
| 118 |
element.removeClass('show'); |
| 119 |
} |
| 120 |
function show_cart_page_preview() { |
| 121 |
var element = $('.merchant-cart-preview'); |
| 122 |
element.addClass('show'); |
| 123 |
} |
| 124 |
function hide_cart_page_preview() { |
| 125 |
var element = $('.merchant-cart-preview'); |
| 126 |
element.removeClass('show'); |
| 127 |
} |
| 128 |
function show_thank_you_page_preview() { |
| 129 |
var element = $('.merchant-thank-you-preview'); |
| 130 |
element.addClass('show'); |
| 131 |
} |
| 132 |
function hide_thank_you_page_preview() { |
| 133 |
var element = $('.merchant-thank-you-preview'); |
| 134 |
element.removeClass('show'); |
| 135 |
} |
| 136 |
function show_checkout_page_preview() { |
| 137 |
var element = $('.merchant-checkout-preview'); |
| 138 |
element.addClass('show'); |
| 139 |
} |
| 140 |
function hide_checkout_page_preview() { |
| 141 |
var element = $('.merchant-checkout-preview'); |
| 142 |
element.removeClass('show'); |
| 143 |
} |
| 144 |
$('.merchant-module-page-setting-box').on('click', function (e) { |
| 145 |
var clickedElement = $(e.target); |
| 146 |
if (clickedElement.closest('.merchant-group-field-cart_page').length > 0 || clickedElement.hasClass('merchant-group-field-cart_page')) { |
| 147 |
show_cart_page_preview(); |
| 148 |
hide_single_product_preview(); |
| 149 |
hide_thank_you_page_preview(); |
| 150 |
hide_checkout_page_preview(); |
| 151 |
} else if (clickedElement.closest('.merchant-group-field-thank_you_page').length > 0 || clickedElement.hasClass('merchant-group-field-thank_you_page')) { |
| 152 |
show_thank_you_page_preview(); |
| 153 |
hide_single_product_preview(); |
| 154 |
hide_cart_page_preview(); |
| 155 |
hide_checkout_page_preview(); |
| 156 |
} else if (clickedElement.closest('.merchant-group-field-checkout_page').length > 0 || clickedElement.hasClass('merchant-group-field-checkout_page')) { |
| 157 |
show_checkout_page_preview(); |
| 158 |
hide_single_product_preview(); |
| 159 |
hide_cart_page_preview(); |
| 160 |
hide_thank_you_page_preview(); |
| 161 |
} else { |
| 162 |
show_single_product_preview(); |
| 163 |
hide_cart_page_preview(); |
| 164 |
hide_thank_you_page_preview(); |
| 165 |
hide_checkout_page_preview(); |
| 166 |
} |
| 167 |
}); |
| 168 |
show_single_product_preview(); |
| 169 |
})(jQuery); |