PageSourceSearch

https://www.hobbyfarms.com/wp-content/themes/Hobby-themes/styles-js/js/cart-checkout.js?v=1.1.5

js hobbyfarms.com collected 2026-09-24 09:19:56 UTC 23,931 bytes, 560 lines download raw bytes

1//////checkout
2if ($('body').hasClass('woocommerce-checkout')) {
3    jQuery(document).ready(function($) { 
4       $('#order_comments').on('input', function() {
5            let value = jQuery(this).val();
6            // Remove leading spaces
7            if (value.startsWith(' ')) {
8                $(this).val(value.trimStart());
9            }
10        });
11            
12        $("#billing_country, #shipping_country").on("change", function() {                          
13            $('body').trigger('update_checkout');
14        });
15        $('.update-cart-btn').on('click', function(e){
16            e.preventDefault();
17            window.location.href = '/cart/';
18        });
19        
20        $('body').on('updated_checkout', function() {
21        
22            $('.custom-summary-loader').show();
23            $('.custom-summary-loader').addClass('active'); // to show 
24            $.ajax({
25                type: 'POST',
26                url: wc_checkout_params.ajax_url,
27                data: {
28                    action: 'get_custom_checkout_summary'
29                },
30                success: function(response) {
31                    if (response.success) {
32                        $('#cart_subtotal').html(response.data.cart_subtotal);
33                        $('#cart_shipping').html(response.data.cart_shipping);
34                        $('#cart_total').html(response.data.cart_total);
35                        $('#coupon_codes').html(response.data.coupon_html);
36                    }
37                    
38                    $('.custom-summary-loader').hide();
39                    $('.custom-summary-loader').removeClass('active'); // to hide
40                }
41                
42            });
43        });
44    
45        // $('.checkout-addon-button').on('click', function(e){
46        //     e.preventDefault();
47        //     $('#spinner-for-cart').show();
48        //     $('.custom-summary-loader-product').show();
49        //     $('.custom-summary-loader-product').addClass('active'); // to show 
50        //     var product_id = $(this).data('product_id');
51            
52        //     $.ajax({
53        //     type: 'POST',
54        //     url: wc_add_to_cart_params.ajax_url,
55        //         data: {
56        //             action: 'woocommerce_ajax_add_to_cart',
57        //             product_id: product_id,
58        //             quantity: 1
59        //         },
60        //         beforeSend: function() {
61        //             // Optional: show loader
62        //         },
63        //         success: function(response) {
64        //             // if (response.error) {
65        //             //     alert('Could not add to cart.');
66        //             // } else {
67        //                 //location.reload();   
68        //                 //$(document.body).trigger('update_checkout');             
69        //             //}
70        //             // $('.custom-summary-loader-product').hide();
71        //             // $('.custom-summary-loader-product').removeClass('active'); // to hide
72        //         },
73        //         error: function() {
74        //             alert('Could not add to cart.');  
75        //             $('.custom-summary-loader-product').hide();
76        //             $('.custom-summary-loader-product').removeClass('active'); //                   
77        //         },
78        //         complete: function () {
79        //             location.reload();                     
80        //         }
81        //     });
82
83        // });
84        // $('body').on('click', '.remove-from-checkout', function(e){
85        //     e.preventDefault();        
86        //     var cartItemKey = $(this).data('cart-item-key');
87        //     var cartItemId  = $(this).data('cart-item-id');
88        //     //var loader = $(this).closest('div').find('.custom-summary-loader-product');
89            
90        //     $('#product-loader-'+cartItemId).show();
91        //     $('#product-loader-'+cartItemId).addClass('active'); // to show          
92
93        //     $.ajax({
94        //         type: 'POST',
95        //         url: '/wp-admin/admin-ajax.php',
96        //         data: {
97        //             action: 'remove_item_from_checkout',
98        //             cart_item_key: cartItemKey
99        //         },
100        //         success: function(response){
101        //             console.log(response)
102        //             if(response.success){
103        //                 // Refresh checkout                   
104        //                 $('#product-' + cartItemId).hide();
105        //                 $(document.body).trigger('update_checkout');                    
106        //             } else {
107        //                 alert('Could not remove item');                    
108        //                 $('#product-loader-'+cartItemId).hide();
109        //                 $('#product-loader-'+cartItemId).removeClass('active'); // to show  
110        //             }
111        //         }
112        //     });
113        // });
114    });
115}
116//////cart
117if (document.body.classList.contains('woocommerce-cart')) {
118    jQuery(document).ready(function ($) {
119        // Loader on checkout click
120        $('.cart-checout-btn').on('click', function () {
121            $('.loader-overlay-cart').css('display', 'flex');
122        });
123        // Remove cart item
124        $(document).on('click', '.fas.fa-trash-alt', function (e) {
125            e.preventDefault();
126
127            const cartItemKey = $(this).data('cart-item-key');
128            const cartItemId = $(this).data('cart-item-id');
129            const cartRow = $(this).data('cart-row');
130
131            const $card = $(this).closest('.cart-card');
132            const $loader = $card.find('.loader-overlay');
133            $loader.css('display', 'flex');
134
135            $.ajax({
136                url: '/wp-admin/admin-ajax.php',
137                type: 'POST',
138                data: {
139                    action: 'my_custom_remove_cart_item',
140                    cart_item_key: cartItemKey
141                },
142                success: function (response) {
143                    if (response.success) {
144                        $('#final_cart_total').html(response.data.cart_total);
145                        $('#cart_total').html(response.data.cart_total);
146                        $('#cart_subtotal').html(response.data.cart_subtotal);
147                        $('#cart_shipping').html(response.data.cart_shipping);
148                        $('#coupon_codes').html(response.data.coupon_html);
149                        $('.product-' + cartItemId+'.row-' + cartRow).hide();
150                    }
151                },
152                error: function () {
153                    alert('AJAX error.');
154                }
155            });
156        });
157
158        // Quantity minus button
159        $(document).on('click', '.btn-qty-minus', function () {
160            const $input = $(this).siblings('.quantity-input');
161            const $card = $(this).closest('.cart-card');
162            const $loader = $card.find('.loader-overlay');
163            const $group = $(this).closest('.input-group');
164
165            let value = parseInt($input.val()) || 0;
166            const minVal = parseInt($input.data('min')) || 1;
167
168            if (value > minVal) {
169                $input.val(value - 1);
170                $loader.css('display', 'flex');
171                $group.addClass('disabled');
172                updateCartQty($input[0], $group, $loader);
173            }
174
175            updateButtons_qty($input[0]);
176        });
177
178        // Quantity plus button
179        $(document).on('click', '.btn-qty-plus', function () {
180            const $input = $(this).siblings('.quantity-input');
181            const $card = $(this).closest('.cart-card');
182            const $loader = $card.find('.loader-overlay');
183            const $group = $(this).closest('.input-group');
184
185            let value = parseInt($input.val()) || 0;
186            const maxVal = parseInt($input.data('max')) || 10;
187
188            if (value < maxVal) {
189                $input.val(value + 1);
190                $loader.css('display', 'flex');
191                $group.addClass('disabled');
192                updateCartQty($input[0], $group, $loader);
193            }
194
195            updateButtons_qty($input[0]);
196        });
197        function updateCartQty(input, $group, $loader) {
198            const cartItemKey      = input.dataset.cartItemKey;
199            const cartproductID    = input.dataset.productId;
200            const cartproductRow   = input.dataset.productRow;
201            const cartproductprice = input.dataset.productPrice;
202            const newQty           = parseInt(input.value) || 1;
203            var tot_val            = cartproductprice * newQty;
204            tot_val                = tot_val.toFixed(2); // e.g. "33.95"
205            // Build FormData for AJAX
206            const data = {
207            action: 'woocommerce_update_cart_item',
208            cart_item_key: cartItemKey,
209            quantity: newQty
210            };
211
212            fetch('/wp-admin/admin-ajax.php', {
213            method: 'POST',
214            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
215            body: new URLSearchParams(data).toString()
216            })
217            .then(response => response.json())
218            .then(data => {			
219                console.log('Cart updated', data);
220
221                if (data.success) {
222                    // Update parts of DOM:
223                    document.getElementById('final_cart_total').innerHTML               = data.data.cart_total;
224                    document.getElementById('cart_total').innerHTML                     = data.data.cart_total;
225                    document.getElementById('cart_subtotal').innerHTML                  = data.data.cart_subtotal;
226                    //document.getElementById('cart_discount').innerHTML                  = '-'+data.data.cart_discount;
227                    document.getElementById('cart_shipping').innerHTML                  = data.data.cart_shipping;	
228                    document.getElementById('coupon_codes').innerHTML                   = data.data.coupon_html;			
229                    document.getElementById(`product-${cartproductID}-price-${cartproductRow}`).innerHTML = `
230                        <span class="woocommerce-Price-amount amount">
231                            <bdi><span class="woocommerce-Price-currencySymbol">$</span>${tot_val}</bdi>
232                        </span>
233                    `;
234                }
235                $loader.css('display', 'none');
236                $group.removeClass('disabled');
237                //location.reload();
238            });
239        }
240    });
241
242    // Yearly input plus/minus buttons (outside jQuery ready)
243    document.addEventListener('DOMContentLoaded', function () {
244        document.querySelectorAll('.btn-yr-plus').forEach(function (button) {
245            button.addEventListener('click', function () {
246                const input = this.parentElement.querySelector('.yearly-input');
247                const $group = $(this).closest('.input-group');
248                const $card = $(this).closest('.cart-card');
249                const $loader = $card.find('.loader-overlay');
250
251                $loader.css('display', 'flex');
252                //$group.addClass('disabled');
253
254                let value = parseInt(input.value) || 0;
255                const maxVal = parseInt(input.dataset.max) || 3;
256
257                if (value < maxVal) {
258                    input.value = value + 1;
259                    updateCartYear(input, $group, $loader);
260                }
261
262                updateButtons(input);
263            });
264            
265        });
266
267        document.querySelectorAll('.btn-yr-minus').forEach(function (button) {
268            button.addEventListener('click', function () {
269                const input = this.parentElement.querySelector('.yearly-input');
270                const $group = $(this).closest('.input-group');
271                const $card = $(this).closest('.cart-card');
272                const $loader = $card.find('.loader-overlay');
273
274                $loader.css('display', 'flex');
275                //$group.addClass('disabled');
276
277                let value = parseInt(input.value) || 0;
278                const minVal = parseInt(input.dataset.min) || 1;
279
280                if (value > minVal) {
281                    input.value = value - 1;
282                    updateCartYear(input, $group, $loader);
283                }
284
285                updateButtons(input);
286            });
287        });
288              
289       
290        function updateCartYear(input, $group, $loader) {
291            var $input = $(input);
292            var cartItemKey      = $input.data('cartItemKey');
293            var cartproductID    = $input.data('productId');
294            var cartproductTerm  = $input.data('productTerm');
295            var cartproductRow   = $input.data('productRow');
296            var cartproductprice = $input.data('productPrice');
297            var newQty           = parseInt($input.val()) || 1;
298            var qtyValue         = parseInt($('.product-' + cartproductID + '.term-' + cartproductTerm).find('input.quantity-input').val(), 10) || 0;
299            var qty_Value        = $('#qty-product-' + cartproductID + '-' + cartproductRow).val();
300
301            var data = {
302                action: 'my_custom_remove_and_add_to_cart',
303                cart_item_key: cartItemKey,
304                product_year: newQty,
305                product_id: cartproductID,
306                qty: qty_Value
307            };
308
309            $.post('/wp-admin/admin-ajax.php', data, function(response) {
310                console.log('cart_item_key:', cartItemKey);
311                console.log('product_year:', newQty);
312                console.log('product_id:', cartproductID);
313                console.log('qty:', qty_Value);
314                if (response.success) {
315                    var newTerm = 'term-' + response.data.product_term_id;
316
317                    var $sameTermInput = $('.product-' + cartproductID + '.' + newTerm).find('input.quantity-input');
318                    var sameTermqty = ($sameTermInput.length > 0) ? parseInt($sameTermInput.val(), 10) || 0 : 0;
319                    //var net_Qty = parseInt(qty_Value) + sameTermqty;
320                    var net_Qty = parseInt(qty_Value);
321                    // console.log('New Value:', net_Qty);
322                    // console.log('sameTermqty:', sameTermqty);
323                    //$('.' + newTerm).hide();
324
325                    $('.product-' + cartproductID + '.term-' + cartproductTerm)
326                        .removeClass('term-' + cartproductTerm)
327                        .addClass(newTerm);
328                    $('#final_cart_total').html(response.data.cart_total);
329                    $('#cart_total').html(response.data.cart_total);
330                    $('#cart_subtotal').html(response.data.cart_subtotal);
331                    $('#coupon_codes').html(response.data.coupon_html);
332                    $('#cart_shipping').html(response.data.cart_shipping);
333
334                    var salePrice        = response.data.cart_product_price;
335                    var regularPrice     = response.data.cart_regular_price;
336                    var cart_check_price = response.data.cart_check_price;
337                    var cart_check_regular = response.data.cart_check_regular;
338
339                    var priceHTML = '';
340                    if (cart_check_price && cart_check_regular > cart_check_price) {
341                        priceHTML += '<span class="text-strike">' + regularPrice + '</span>&nbsp; &nbsp;<span>' + salePrice + '</span>';
342                    } else {
343                        priceHTML += '<span>' + salePrice + '</span>';
344                    }
345
346                    $('#both-' + cartproductID + '-price-' + cartproductRow).html(priceHTML);
347                    $('#product-' + cartproductID + '-price-' + cartproductRow).html('$' + (cart_check_price * net_Qty).toFixed(2));
348                    
349                    $input.data('cartItemKey', response.data.cart_item_key);
350                    $('#qty-product-' + cartproductID + '-' + cartproductRow).data('productPrice', cart_check_price);
351                    $('i[data-cart-item-id="' + cartproductID + '"][data-cart-row="' + cartproductRow + '"]').attr('data-cart-item-key', response.data.cart_item_key);
352                    //$('#remove-item-' + cartproductID).data('cartItemKey', response.data.cart_item_key);
353                    $('#qty-product-' + cartproductID + '-' + cartproductRow).data('cartItemKey', response.data.cart_item_key);
354
355                    const $container = $('#qty-' + cartproductID + '-' + cartproductRow);
356                     
357
358                    $container.html(`
359                        <button class="btn btn-outline-secondary btn-sm btn-qty-minus" type="button">-</button>
360                        <input type="text" id="qty-product-${cartproductID}-${cartproductRow}" 
361                            class="form-control form-control-sm text-center quantity-input" 
362                            value="${net_Qty}" 
363                            data-min="1" 
364                            data-max="10" 
365                            data-product-row="${cartproductRow}" 
366                            data-cart-item-key="${response.data.cart_item_key}" 
367                            data-product-id="${cartproductID}" 
368                            data-product-price="${cart_check_price}" 
369                        >
370                        <button class="btn btn-outline-secondary btn-sm btn-qty-plus" type="button">+</button>
371                    `);
372                     
373                    $loader.hide();
374                    //$group.removeClass('disabled');
375                }
376            });
377        }
378
379        // $(document).on('click', '.btn-yr-plus', function () {
380
381        //     const $input = $(this).closest('.input-group').find('.yearly-input');
382        //     const $card  = $(this).closest('.cart-card');
383        //     const $loader = $card.find('.loader-overlay');
384        //     const $group = $(this).closest('.input-group');
385
386        //     let value  = parseInt($input.val()) || 0;
387        //     let maxVal = parseInt($input.data('max')) || 3;
388
389        //     if(value >= maxVal) return;
390
391        //     value++;
392        //     $input.val(value);
393
394        //     $loader.css('display','flex');
395        //     //$group.addClass('disabled');
396
397        //     updateCartYear($input,$group,$loader);
398
399        // });
400        // $(document).on('click', '.btn-yr-minus', function () {
401
402        //     const $input = $(this).closest('.input-group').find('.yearly-input');
403        //     const $card  = $(this).closest('.cart-card');
404        //     const $loader = $card.find('.loader-overlay');
405        //     const $group = $(this).closest('.input-group');
406
407        //     let value  = parseInt($input.val()) || 0;
408        //     let minVal = parseInt($input.data('min')) || 1;
409
410        //     if(value <= minVal) return;
411
412        //     value--;
413        //     $input.val(value);
414
415        //     $loader.css('display','flex');
416        //     //$group.addClass('disabled');
417
418        //     updateCartYear($input,$group,$loader);
419
420        // });
421        // function updateCartYear(input, $group, $loader){
422
423        //     var $input = $(input);
424
425        //     var cartItemKey   = $input.data('cartItemKey');
426        //     var cartproductID = $input.data('productId');
427        //     var cartproductTerm = $input.data('productTerm');
428        //     var cartproductRow  = $input.data('productRow');
429
430        //     var newQty = parseInt($input.val()) || 1;
431
432        //     var qty_Value = parseInt($('#qty-product-' + cartproductID + '-' + cartproductRow).val()) || 0;
433
434        //     var data = {
435        //         action: 'my_custom_remove_and_add_to_cart',
436        //         cart_item_key: cartItemKey,
437        //         product_year: newQty,
438        //         product_id: cartproductID,
439        //         qty: qty_Value
440        //     };
441
442        //     $.post(ajaxurl, data, function(response){
443
444        //         if(response.success){
445
446        //             var data = response.data;
447
448        //             var newTerm = 'term-' + data.product_term_id;
449
450        //             var salePrice        = data.cart_product_price;
451        //             var regularPrice     = data.cart_regular_price;
452        //             var cart_check_price = data.cart_check_price;
453        //             var cart_check_regular = data.cart_check_regular;
454
455        //             $('#final_cart_total').html(data.cart_total);
456        //             $('#cart_total').html(data.cart_total);
457        //             $('#cart_subtotal').html(data.cart_subtotal);
458        //             $('#coupon_codes').html(data.coupon_html);
459        //             $('#cart_shipping').html(data.cart_shipping);
460
461        //             console.log('Success Data:', data);
462
463        //         } else {
464
465        //             console.log('Cart update failed');
466
467        //         }
468
469        //     }).fail(function(){
470
471        //         console.log('Ajax error');
472
473        //     }).always(function(){
474
475        //         $loader.hide();
476        //         $group.removeClass('disabled');
477
478        //     });
479
480        // }
481        document.querySelectorAll('.yearly-input').forEach(updateButtons);
482        document.querySelectorAll('.quantity-input').forEach(updateButtons_qty);
483    });
484
485
486    function updateButtons(input) {
487        const value = parseInt(input.value) || 0;
488        const min = parseInt(input.dataset.min) || 1;
489        const max = parseInt(input.dataset.max) || 3;
490
491        const minusBtn = input.parentElement.querySelector('.btn-yr-minus');
492        const plusBtn = input.parentElement.querySelector('.btn-yr-plus');
493
494        minusBtn.disabled = value <= min;
495        plusBtn.disabled = value >= max;
496    }
497
498    function updateButtons_qty(input) {
499        const value = parseInt(input.value) || 0;
500        const min = parseInt(input.dataset.min) || 1;
501        const max = parseInt(input.dataset.max) || 10;
502
503        const minusBtn = input.parentElement.querySelector('.btn-qty-minus');
504        const plusBtn = input.parentElement.querySelector('.btn-qty-plus');
505
506        if (minusBtn) minusBtn.disabled = value <= min;
507        if (plusBtn) plusBtn.disabled = value >= max;
508    }
509
510} // end if woocommerce-cart
511
512jQuery(document).ready(function($) {    
513    $('#promo_code').on('keyup', function() {
514        $('#coupon_message').empty();
515    });
516   $('#apply_coupon').on('click', function(e) {
517		e.preventDefault();
518		let coupon_code = $('#promo_code').val();
519		 
520		let $button = $(this);
521		$('#coupon_message').html('');
522
523		if (coupon_code === '') {
524			$('#coupon_message').html('<div class="text-danger">❌ Please enter a coupon code.</div>');
525			return; // ⛔️ Stop here — don’t run AJAX
526		}
527		// Add loading state
528		$button.addClass('btn-loading');
529
530		$.ajax({
531			url: '/wp-admin/admin-ajax.php',
532			type: 'POST',
533			data: {
534				action: 'apply_coupon_code',
535				coupon_code: coupon_code,
536			},
537			success: function(response) {
538				// console.log(response);
539				if (response.success) {					 
540					document.getElementById('cart_total').innerHTML       = response.data.cart_total;
541					document.getElementById('cart_subtotal').innerHTML    = response.data.cart_subtotal;
542					//document.getElementById('cart_discount').innerHTML    = response.data.cart_discount;
543					document.getElementById('cart_shipping').innerHTML    = response.data.cart_shipping;	
544					document.getElementById('coupon_codes').innerHTML     = response.data.coupon_html;
545					
546					$('#coupon_message').html('<div class="text-success">✅ Coupon applied successfully!</div>');
547				} else {
548					$('#coupon_message').html('<div class="text-danger">⚠️ Invalid coupon.</div>');
549				}
550			},
551			error: function() {
552				$('#coupon_message').html('<div class="text-danger">⚠️ Something went wrong.</div>');
553			},
554			complete: function() {
555				// Remove loading state
556				$button.removeClass('btn-loading');
557			}
558		});
559   });
560});

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.