PageSourceSearch

https://area52.com/wp-content/plugins/wt-smart-coupon-pro/public/js/wt-smart-coupon-public.js?ver=3.6.1

js area52.com collected 2026-10-03 19:44:32 UTC 11,785 bytes, 327 lines download raw bytes

1jQuery(function ($) {
2    "use strict";
3    $('form.checkout').on('change.wt_sc_payment_method_change', 'input[name="payment_method"]', function () {
4        
5        var t = {updateTimer: !1, dirtyInput: !1,
6            reset_update_checkout_timer: function () {
7                clearTimeout(t.updateTimer)
8            }, trigger_update_checkout: function () {
9                t.reset_update_checkout_timer(), t.dirtyInput = !1,
10                $(document.body).trigger("update_checkout");
11            }
12        };
13        t.trigger_update_checkout();
14    });
15
16    $('document').ready(function(){
17        
18        $('input[name="wt_coupon_to_do"]').on('change',function() {
19            var val = $(this).val();
20            if( val == 'gift_to_a_friend' ) {
21                $('.gift_to_friend_form').show()
22            } else {
23                $('.gift_to_friend_form').hide()
24            }
25        });
26
27        /* After the coupon box click event was done */
28        $(document).on("wt_sc_api_coupon_click_done", function(e){
29            wt_unblock_node($( 'div.wt_coupon_wrapper'));
30            wt_unblock_node($("div.wt-mycoupons"));
31            wt_unblock_node($("div.wt_store_credit"));
32        });
33
34        /** Handle keyboard Enter press on coupons */
35        $( document ).on("keypress", '.wt-single-coupon.active-coupon', function(e){
36            if( 13 === e.which ) { 
37                $( this ).trigger( 'click' );
38            }
39        } );
40
41        $(document).on("click",'.wt-single-coupon.active-coupon', function(){
42            
43            if(!$('.woocommerce-notices-wrapper').length){
44                $('#main').prepend('<div class="woocommerce-notices-wrapper"></div>');
45            }
46
47            const coupon_code = ( $(this).attr('data-code') === undefined ? $(this).find('code').text() : $(this).attr('data-code') );
48            const coupon_id = $(this).attr('data-id');
49
50            $('div.wt_coupon_wrapper, div.wt-mycoupons, .div.wt_store_credit').each(function(){
51                if($(this).find('.wt-single-coupon').length)
52                {
53                    wt_block_node($(this));
54                }
55            });
56
57            /* For checkout block compatibility */
58            if( $('.wc-block-checkout, .wc-block-cart').length ) {
59                
60                const coupon_click_event = new CustomEvent("wt_sc_api_coupon_clicked", {
61                    detail:{ 'coupon_code' : coupon_code, 'coupon_id': coupon_id}
62                });
63                document.dispatchEvent(coupon_click_event);
64                return false;
65            }
66            
67            var data = {
68                'coupon_code'   : coupon_code,
69                'coupon_id'     : coupon_id,
70                '_wpnonce'      : WTSmartCouponOBJ.nonces.apply_coupon
71            };
72    
73            $.ajax({
74                type: "POST",
75                async: true,
76                url: WTSmartCouponOBJ.wc_ajax_url + 'apply_coupon_on_click',
77                data: data,
78                success: function (response) {
79                    if ( $( '.woocommerce-cart-form' ).length != 0 ) {
80                        update_cart(true);  // need only for cart page
81                    }
82                    
83                    wt_unblock_node( $( 'div.wt_coupon_wrapper' ) );
84                    wt_unblock_node($("div.wt-mycoupons"));
85                    wt_unblock_node($("div.wt_store_credit"));
86
87                    $( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
88                    show_notice( response );
89                    $(document.body).trigger("update_checkout");
90                    $( document.body ).trigger( "applied_coupon", [ coupon_code ] );
91
92                    $('html, body').animate({
93                        scrollTop: $(".woocommerce").offset().top
94                    }, 1000);
95                }
96            });
97        });
98
99        $('.wt_sc_credit_history').on('click',function( e ){ e.stopPropagation(); })
100
101
102        /* For checkout block */
103        if( $('.wc-block-checkout').length ) {
104            $( document ).on("click", '[name="radio-control-wc-payment-method-options"]', function(){
105                WTSmartCouponOBJ.payment_method = $('[name="radio-control-wc-payment-method-options"]:checked').val();
106                let parent_div = $('[name="radio-control-wc-payment-method-options"]').parents('.wc-block-components-radio-control');
107                parent_div.find('.wc-block-components-radio-control__option').removeClass('wc-block-components-radio-control__option-checked');
108                wbte_set_block_checkout_values();
109            });
110
111            setTimeout( wbte_set_block_checkout_values, 200);
112        }
113
114    });
115
116    /** Update payment method on session, if coupons are changed update checkout */
117    $( document.body ).on( 'payment_method_selected', 
118        function() {
119            jQuery( document ).on( "updated_checkout", 
120                function(){
121                    const selectedPaymentMethod = $( 'form.checkout' ).find( 'input[name="payment_method"]:checked' ).val();
122                    $.ajax({
123                        type: "POST",
124                        url: WTSmartCouponOBJ.wc_ajax_url + 'wbte_sc_update_payment_method_on_session',
125                        data: {
126                            'payment_method': selectedPaymentMethod,
127                            '_wpnonce': WTSmartCouponOBJ.nonces.public
128                        },
129                        success: function( response ) {
130                            if ( response ) {
131                                $( 'form.checkout' ).trigger( 'update' );
132                            }
133                        }
134                    });
135                }
136            );
137        }
138    );
139
140    var wbte_set_block_checkout_values = function() {
141
142        let payment_method = '';
143        let shipping_method = {};
144      
145        if( $('[name="radio-control-wc-payment-method-options"]').length ) {
146            
147            /* Prepare payment method from radio button */
148            payment_method = $('[name="radio-control-wc-payment-method-options"]:checked').val();           
149
150        }
151        
152        /* Prepare shipping method from radio button */
153        if( $('.wc-block-components-shipping-rates-control input[type="radio"]').length ) {         
154            $('.wc-block-components-shipping-rates-control input[type="radio"]:checked').each(function(index){
155                shipping_method[index] = $(this).val(); 
156            });
157        }
158
159        /* Store the value to global variable to prevent future auto refresh blocking */
160        WTSmartCouponOBJ.shipping_method = shipping_method;
161        WTSmartCouponOBJ.payment_method = payment_method;
162
163        
164        /* Send ajax request to set the value */
165        let order_summary_block = $('.wp-block-woocommerce-checkout-order-summary-block');
166        wt_block_node( order_summary_block );
167        
168        $.ajax({
169            type: "POST",
170            async: true,
171            url: WTSmartCouponOBJ.wc_ajax_url + 'wbte_sc_set_block_checkout_values',
172            data: { '_wpnonce': WTSmartCouponOBJ.nonces.public, 'payment_method': payment_method, 'shipping_method': shipping_method },
173            dataType: 'json',
174            success:function( data ) {
175                wt_unblock_node( order_summary_block );
176            },
177            error:function() {
178               wt_unblock_node( order_summary_block ); 
179            }
180        });
181
182        /* Trigger checkout block refresh */
183        setTimeout(function(){ 
184            const checkout_value_updated_event = new CustomEvent("wbte_sc_checkout_value_updated", {
185                detail:{ 'payment_method' : payment_method, 'shipping_method': shipping_method }
186            });
187            document.dispatchEvent(checkout_value_updated_event);
188        }, 1000);
189        
190    }
191
192
193
194    var show_notice = function( html_element, $target ) {
195		if ( ! $target ) {
196			$target = $( '.woocommerce-notices-wrapper:first' ) || $( '.cart-empty' ).closest( '.woocommerce' ) || $( '.woocommerce-cart-form' );
197		}
198		$target.prepend( html_element );
199    };
200    
201    /**
202     * Function directly using from cart.js by woocommmerce
203     * @param {bool} preserve_notices 
204     */
205    var update_cart = function( preserve_notices ) {
206        var $form = $( '.woocommerce-cart-form' );
207        wt_block_node( $form );
208        wt_block_node( $( 'div.cart_totals' ) );
209        
210        
211
212        // Make call to actual form post URL.
213        $.ajax( {
214            type:     $form.attr( 'method' ),
215            url:      $form.attr( 'action' ),
216            data:     $form.serialize(),
217            dataType: 'html',
218            success:  function( response ) {
219                update_wc_div( response, preserve_notices );
220            },
221            complete: function() {
222                wt_unblock_node( $form );
223                wt_unblock_node( $( 'div.cart_totals' ) );
224            }
225        } );
226    }
227
228    /**
229     * function directley used from cart.js by wooocommerce
230     * @param { jQuery object }
230 node 
231     */
232    var wt_block_node = function( node ) {
233        node.addClass( 'processing' );
234
235        if(typeof $.fn.block === 'function')
236        {
237            node.block({
238                message: null,
239                overlayCSS: {
240                    background: '#fff',
241                    opacity: 0.6
242                }
243            });
244        }
245    }
246    globalThis.wbte_sc_block_node = wt_block_node;
247
248    /**
249     * function directley used from cart.js by wooocommerce
250     * @param {jQuery object} $node 
251     */
252    var wt_unblock_node = function( node ) {
253		node.removeClass( 'processing' );
254        
255        if(typeof $.fn.unblock === 'function')
256        {
257            node.unblock();
258        }
259    };
260    globalThis.wbte_sc_unblock_node = wt_unblock_node;
261    
262    /**
263     * 
264     * @param {string} html_str 
265     * @param {bool} preserve_notices 
266     */
267    var update_wc_div = function( html_str, preserve_notices ) {
268		var $html       = $.parseHTML( html_str );
269		var $new_form   = $( '.woocommerce-cart-form', $html );
270		var $new_totals = $( '.cart_totals', $html );
271		var $notices    = $( '.woocommerce-error, .woocommerce-message, .woocommerce-info', $html );
272
273		// No form, cannot do this.
274		if ( $( '.woocommerce-cart-form' ).length === 0 ) {
275			return;
276		}
277
278		// Remove errors
279		if ( ! preserve_notices ) {
280			$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
281		}
282
283		if ( $new_form.length === 0 ) {
284			// If the checkout is also displayed on this page, trigger reload instead.
285			if ( $( '.woocommerce-checkout' ).length ) {
286				return;
287			}
288
289			// No items to display now! Replace all cart content.
290			var $cart_html = $( '.cart-empty', $html ).closest( '.woocommerce' );
291			$( '.woocommerce-cart-form__contents' ).closest( '.woocommerce' ).replaceWith( $cart_html );
292
293			// Display errors
294			if ( $notices.length > 0 ) {
295				show_notice( $notices );
296			}
297		} else {
298			// If the checkout is also displayed on this page, trigger update event.
299			if ( $( '.woocommerce-checkout' ).length ) {
300				$( document.body ).trigger( 'update_checkout' );
301			}
302
303			$( '.woocommerce-cart-form' ).replaceWith( $new_form );
304			$( '.woocommerce-cart-form' ).find( ':input[name="update_cart"]' ).prop( 'disabled', true );
305
306			if ( $notices.length > 0 ) {
307				show_notice( $notices );
308			}
309
310			update_cart_totals_div( $new_totals );
311		}
312
313		$( document.body ).trigger( 'updated_wc_div' );
314    };
315    /**
316     * Function directley using from woocmmerce cart.js
317     * @param {string} html_str 
318     */
319    var update_cart_totals_div = function( html_str ) {
320		$( '.cart_totals' ).replaceWith( html_str );
321		$( document.body ).trigger( 'updated_cart_totals' );
322    };
323    
324    
325});
326
327

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.