PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.11.1
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.11.1
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / assets / js / modules / volume-discounts / admin / preview.js

preview.js in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.11.1, at assets/js/modules/volume-discounts/admin/preview.js

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