PageSourceSearch

https://trailsports.com/wp-content/plugins/divi-bodycommerce/js/add-to-cart-ajax.min.js

js trailsports.com collected 2026-10-08 15:56:53 UTC 20,885 bytes, 453 lines download raw bytes

1jQuery(function ($) {
2
3    $(".bodycommerce-minicart").prependTo(".shop-cart");
4
5
6    $(".post-type-archive-product").on("click", ".quantity input", function () {
7        return false;
8    });
9
10    $(".archive").on("change input", ".quantity .qty", function () {
11        var add_to_cart_button = $(this).parents(".product").find(".add_to_cart_button, .ajax_add_to_cart");
12        add_to_cart_button.data("quantity", $(this).val());
13        add_to_cart_button.attr("href", "?add-to-cart=" + add_to_cart_button.attr("data-product_id") + "&quantity=" + $(this).val());
14
15    });
16
17
18    $(".input-text.qty.text").bind("keyup mouseup", function () {
19        var value = $(this).val();
20        $(".product_quantity").val(value);
21    });
22
23
24    if (typeof wc_add_to_cart_params === 'undefined')
25        return false;
26
27    $(".ajax_add_to_cart").removeClass('add_to_cart_button');
28
29    /**
30     * Scroll-to-top after AJAX add is for WooCommerce notices; skip if button has
31     * disable_scroll_update or the click came from a PL Add To Cart module with scroll disabled (.bc-atc-no-scroll).
32     */
33    function bc_atc_should_scroll_to_top($btn) {
34        if (!$btn || !$btn.length) {
35            return true;
36        }
37        if ($btn.hasClass('disable_scroll_update')) {
38            return false;
39        }
40        if ($btn.closest('.bc-atc-no-scroll').length) {
41            return false;
42        }
43        return true;
44    }
45    
46    $(document).on("click", ".ajax_add_to_cart", function (e) {
47        e.preventDefault();
48        e.stopPropagation();
49
50        if ( $(this).hasClass('wc-variation-is-unavailable') ) {
51            return;
52        }
53
54        var thisbutton = $(this);
55        var $cartForm = thisbutton.closest('form.cart');
56        var formdata = $cartForm.serializeArray();
57
58        if ($(".db_atc_notify_txt").length) {
59            var db_atc_notify_txt = $(".db_atc_notify_txt").val(),
60                db_atc_notify_txt_time = $(".db_atc_notify_txt_time").val(),
61                db_atc_notify_bg_color = $(".db_atc_notify_bg_color").val(),
62                db_atc_notify_text_color = $(".db_atc_notify_text_color").val(),
63                thisbutton_text = thisbutton.text();
64            thisbutton.attr("style", "color:" + db_atc_notify_text_color + " !important; background-color:" + db_atc_notify_bg_color + " !important;"),
65                thisbutton.text(db_atc_notify_txt), thisbutton.addClass("active"),
66                setTimeout(function () {
67                    thisbutton.text(thisbutton_text), thisbutton.removeClass("active"), thisbutton.removeAttr("style");
68                }, db_atc_notify_txt_time);
69        }
70
71        if ( thisbutton.closest('.et_pb_db_shop_loop').length > 0 ) {
72            $('.et_pb_db_shop_loop').find('.woocommerce-notices-wrapper').removeClass('main');
73            $('.et_pb_db_shop_loop').find('.woocommerce-notices-wrapper').html('');
74            thisbutton.closest('.et_pb_db_shop_loop').find('.woocommerce-notices-wrapper').addClass('main');
75        }
76
77        var grouped_form = $(this).closest('.grouped_form');
78        var variation_form = $(this).closest('.variations_form');
79        $(".ajaxerrors").remove();
80        var item = {}, // was _
81            check = true; // was s
82        variations = variation_form.find('select[name^=attribute]');
83
84        if (!variations.length) {
85            variations = variation_form.find('[name^=attribute]:checked');
86        }
87
88        if (!variations.length) {
89            variations = variation_form.find('input[name^=attribute]');
90        }
91
92        var ajax_error_text = bc_add_to_cart_params.ajax_error_text;
93
94        variations.each(function () {
95            var $this = $(this),
96                attributeName = $this.attr('name'),
97                attributevalue = $this.val(),
98                index,
99                attributeTaxName;
100            $this.removeClass('error');
101
102            if (attributevalue.length === 0) {
103                index = attributeName.lastIndexOf('_');
104                attributeTaxName = attributeName.substring(index + 1);
105                $this
106                    .addClass('required error')
107                    .before('<div class="ajaxerrors"><p>' + ajax_error_text + ' ' + attributeTaxName + '</p></div>');
108                check = false;
109            } else {
110                item[attributeName] = attributevalue;
111            }
112        });
113
114        if (!check) {
115            return false;
116        }
117
118        if (thisbutton.is(".ajax_add_to_cart")) {
119            thisbutton.removeClass('added');
120            thisbutton.addClass('loading');
121
122            if ($(this).parents(".variations_form")[0]) { // If variation product
123                // console.log("variation add to cart");
124                var product_id = variation_form.find('input[name=product_id]').val();
125                var quantity = variation_form.find('input[name=quantity]').val();
126                var var_id = variation_form.find('input[name=variation_id]').val();
127                var data = {
128                    action: 'bodycommerce_ajax_add_to_cart_woo',
129                    product_id: product_id,
130                    quantity: quantity,
131                    variation_id: var_id,
132                    variation: item
133                };
134
135                // console.log(data);
136
137                var vanilla_woo = "yes";
138
139            } else if ($(this).parents(".grouped_form")[0]) { // if grouped product
140                // console.log("Grouped add to cart");
141                var main_product_id = grouped_form.find('input[name=add-to-cart]').val();
142                var grouped_items_data = {};
143                var grouped_items = grouped_form.find('.woocommerce-grouped-product-list-item');
144                // var quantity = variation_form.find('input[name=quantity]').val();
145
146                grouped_items.each(function () {
147                    var $this = $(this),
148                        grouped_product_id = $this.attr('id'),
149                        quantity = $this.find('.qty').val();
150
151                    grouped_items_data[grouped_product_id] = quantity;
152                });
153
154                var data = {
155                    action: 'bodycommerce_ajax_add_to_cart_woo_bundle',
156                    product_id: main_product_id,
157                    quantity: "0",
158                    grouped_data: grouped_items_data
159                };
160
161                var vanilla_woo = "no";
162            } else if ($cartForm.length && (
163                $cartForm.hasClass('bundle_form') ||
164                $cartForm.hasClass('yith-wcpb-bundle-form') ||
165                $cartForm.find('[name^="bundled_items"], [name*="bundled_items["], [name^="yith_bundle_"]').length
166            )) {
167                // WooCommerce Product Bundles + YITH WCPB: send full form as urlencoded POST so bundle fields reach PHP (flat $.post objects can drop or flatten data).
168                var bundlePost = $cartForm.serialize();
169                var bundlePid = $cartForm.find('input[name="add-to-cart"]').val();
170                if (typeof bundlePid === 'undefined' || bundlePid === '' || bundlePid === null) {
171                    bundlePid = $cartForm.attr('data-product-id');
172                }
173                if (typeof bundlePid === 'undefined' || bundlePid === '' || bundlePid === null) {
174                    bundlePid = typeof $(this).attr('value') !== 'undefined' ? $(this).attr('value') : $(this).val();
175                }
176                if (bundlePid) {
177                    bundlePost += (bundlePost.length ? '&' : '') + 'product_id=' + encodeURIComponent(bundlePid);
178                }
179                bundlePost += (bundlePost.length ? '&' : '') + 'action=' + encodeURIComponent('bodycommerce_ajax_add_to_cart_woo_single');
180                var data = bundlePost;
181                var vanilla_woo = "bundle_serialized";
182            } else {
183                // Resolve product_id for the clicked button. Order matters: each loop item has its own
184                // .et_pb_db_atc_N wrapper, so a global ".et_pb_db_atc_0 .button" lookup must be the LAST
185                // resort, otherwise every click in a loop would resolve to the first product (SUP-24714).
186                var product_id;
187
188                // 1) Hidden .product_id input next to the button (legacy single-product layouts).
189                product_id = $(this).parent().find(".product_id").val();
190
191                // 2) Button's own value attribute / val(): <button name="add-to-cart" value="371">.
192                //    This is the canonical WooCommerce source and is correct in archive/loop output.
193                if (typeof product_id === 'undefined' || product_id === '' || product_id === null) {
194                    if (typeof thisbutton.attr('value') !== 'undefined' && thisbutton.attr('value') !== '') {
195                        product_id = thisbutton.attr('value');
196                    } else if (typeof thisbutton.val() !== 'undefined' && thisbutton.val() !== '') {
197                        product_id = thisbutton.val();
198                    }
199                }
200
201                // 3) data-product_id (WooCommerce loop <a class="ajax_add_to_cart" data-product_id="…">).
202                if (typeof product_id === 'undefined' || product_id === '' || product_id === null) {
203                    product_id = thisbutton.attr('data-product_id');
204                }
205
206                // 4) Hidden input[name="add-to-cart"] inside the cart form.
207                if (typeof product_id === 'undefined' || product_id === '' || product_id === null) {
208                    product_id = $cartForm.find('input[name="add-to-cart"]').val();
209                }
210
211                // 5) Closest .et_pb_db_atc wrapper (PL Add to Cart module) — scoped to THIS click.
212                if (typeof product_id === 'undefined' || product_id === '' || product_id === null) {
213                    product_id = thisbutton.closest('.et_pb_db_atc').find('.product_id').val();
214                }
215
216                // 6) Last resort: first .et_pb_db_atc_0 module on the page. Only meaningful on a
217                //    single-product page; in archive/loop pages this would hit the wrong product.
218                if (typeof product_id === 'undefined' || product_id === '' || product_id === null) {
219                    product_id = $(".et_pb_db_atc_0 .button").val();
220                }
221
222                if ( $(this).hasClass('wc-bookings-booking-form-button') ) {
223                	product_id = $(this).parent().find('.wc-booking-product-id').val();
224                }
225
226                var quantity = $(this).parent().find(".qty").val();
227                if (typeof quantity === 'undefined' || quantity === '' || quantity === null) {
228                    quantity = thisbutton.attr('data-quantity');
229                }
230                if (typeof quantity === 'undefined' || quantity === '' || quantity === null) {
231                    quantity = thisbutton.closest('.et_pb_db_atc').find('.qty').val();
232                }
233                if (typeof quantity === 'undefined' || quantity === '' || quantity === null) {
234                    quantity = '1';
235                }
236                var data = {
237                    action: 'bodycommerce_ajax_add_to_cart_woo_single',
238                    product_id: product_id,
239                    quantity: quantity
240                };
241                var vanilla_woo = "yes";
242            }
243
244            if (vanilla_woo !== "bundle_serialized") {
245                $.each(formdata, function (key, f_data) {
246                    if (f_data.name != 'add-to-cart') {
247                        data[f_data.name] = f_data.value;
248                    }
249                });
250            }
251
252            if (vanilla_woo !== "bundle_serialized" && data.hasOwnProperty('subscribe-to-action-input') && data['subscribe-to-action-input'] == 'no') {
253                data['convert_to_sub_' + product_id] = "0";
254            }
255
256            if (vanilla_woo == "yes" || vanilla_woo == "bundle_serialized") {
257                if ((typeof window.bc_atc_debug !== 'undefined' && window.bc_atc_debug) || (window.location.search && window.location.search.indexOf('bc_atc_debug=1') !== -1)) {
258                    console.log('[BodyCommerce ATC]', { vanilla_woo: vanilla_woo, payload: data });
259                }
260                $('body').trigger('adding_to_cart', [thisbutton, data]);
261                $.post(wc_add_to_cart_params.ajax_url, data, function (response) {
262                    if (!response)
263                        return;
264
265                    if ( typeof response == 'string' ) {
266                        response = JSON.parse(response);    
267                    }                    
268
269                    var this_page = window.location.toString();
270                    this_page = this_page.replace('add-to-cart', 'added-to-cart');
271
272                    if (response.error && response.product_url) {
273                        //window.location = response.product_url;
274                        //return;
275                        if ($('.woocommerce-notices-wrapper.main').length > 0) {
276                            $('.woocommerce-notices-wrapper.main').html(response.error_message);
277                        } else {
278                            if ( $('.et_pb_db_notices').length > 0 ) {
279                                $('.et_pb_db_notices').html( '<div class="woocommerce-notices-wrapper">' + response.error_message + '</div>');
280                            }
281                        }
282                        thisbutton.removeClass('loading');
283                        if (bc_atc_should_scroll_to_top(thisbutton)) {
284                            // scroll to notices
285                            $('html, body').animate({
286                                scrollTop: 0
287                            }, 300);
288                        }
289
290                        return;
291                    }
292
293                    if (wc_add_to_cart_params.cart_redirect_after_add === 'yes') {
294                        window.location = wc_add_to_cart_params.cart_url;
295                        return;
296                    } else {
297                        thisbutton.removeClass('loading');
298                        var fragments = response.fragments;
299                        var cart_hash = response.cart_hash;
300
301                        if (fragments) {
302                            $.each(fragments, function (key) {
303                                $(key).addClass('updating');
304                            });
305                        }
306
307                        $('.shop_table.cart, .updating, .cart_totals').fadeTo('400', '0.6').block({
308                            message: null,
309                            overlayCSS: {
310                                opacity: 0.6
311                            }
312                        });
313
314                        thisbutton.addClass('added');
315
316                        if (fragments) {
317                            $.each(fragments, function (key, value) {
318                                if ( key == 'div.woocommerce-notices-wrapper.main' ) {
319                                    if ($('.woocommerce-notices-wrapper.main').length > 0) {
320                                        $('.woocommerce-notices-wrapper.main').html(value);
321                                    } else if ( $('.woocommerce-notices-wrapper').length > 0 ) {
322                                        $('.woocommerce-notices-wrapper').html(value);
323                                    } else {
324                                        if ( $('.et_pb_db_notices').length > 0 ) {
325                                            $('.et_pb_db_notices').html( '<div class="woocommerce-notices-wrapper">' + value + '</div>');
326                                        }
327                                    }
328                                }
329                                
330                                $(key).replaceWith(value);
331                            });
332                        }
333
334                        $('.widget_shopping_cart, .updating').stop(true).css('opacity', '1').unblock();
335                        $('.shop_table.cart').load(this_page + ' .shop_table.cart:eq(0) > *', function () {
336                            $('.shop_table.cart').stop(true).css('opacity', '1').unblock();
337                            $(document.body).trigger('cart_page_refreshed');
338                        });
339
340                        $("#bodycommerce_added_to_cart_popup").addClass("active");
341                        $('.cart_totals').load(this_page + ' .cart_totals:eq(0) > *', function () {
342                            $('.cart_totals').stop(true).css('opacity', '1').unblock();
343                        });
344
345                        if (bc_atc_should_scroll_to_top(thisbutton)) {
346                            // scroll to notices
347                            $('html, body').animate({
348                                scrollTop: 0
349                            }, 300);
350                        }
351
352                        $(document).trigger('bodycommerce_atc_completed');
353                    }
354                });
355                return false;
356            } else {
357
358                jQuery.ajax({
359                    url: ajax_object.ajax_url,
360                    data: data,
361                    type: 'POST',
362                    dataType: 'JSON',
363                    beforeSend: function (xhr) {
364                    },
365                    success: function (response) {
366                        // console.log("sucess!");
367
368                        if (!((typeof response.error != 'undefined') && response.error == true)) {
369                            var fragments = response.fragments;
370                            var cart_hash = response.cart_hash;
371                            $('.shop_table.cart, .updating, .cart_totals').fadeTo('400', '0.6').block({
372                                message: null,
373                                overlayCSS: {
374                                    opacity: 0.6
375                                }
376                            });
377
378                            if (fragments) {
379                                $.each(fragments, function (key, value) {
380                                    $(key).replaceWith(value);
381                                });
382                            }
383
384                            $('.widget_shopping_cart, .updating').stop(true).css('opacity', '1').unblock();
385                            $('.shop_table.cart').load(this_page + ' .shop_table.cart:eq(0) > *', function () {
386                                $('.shop_table.cart').stop(true).css('opacity', '1').unblock();
387                                $(document.body).trigger('cart_page_refreshed');
388                            });
389
390                            $("#bodycommerce_added_to_cart_popup").addClass("active");
391
392                            $('.cart_totals').load(this_page + ' .cart_totals:eq(0) > *', function () {
393                                $('.cart_totals').stop(true).css('opacity', '1').unblock();
394                            });
395
396                            
397
398                            $(document).trigger('bodycommerce_atc_completed');
399                        } else {
400                            if ($('.woocommerce-notices-wrapper').length > 0) {
401                                $('.woocommerce-notices-wrapper').html(response.error_message);
402                            } else {
403                                if ( $('.et_pb_db_notices').length > 0 ) {
404                                    $('.et_pb_db_notices').html( '<div class="woocommerce-notices-wrapper">' + response.error_message + '</div>');
405                                }
406                            }
407                        }
408                    },
409                    error: function (jqXHR, exception) {
410                        console.log("error!");
411                    },
412                });
413            }
414        } else {
415            return true;
416        }
417    });
418
419    jQuery(document).on('applied_coupon', function(){
420        jQuery('html, body').animate({
421            scrollTop: 0
422        }, 300);
423    });
424
425    jQuery(document).on('removed_coupon', function(){
426        jQuery('html, body').animate({
427            scrollTop: 0
428        }, 300);
429    });
430
431    jQuery(document).on('applied_coupon_in_checkout', function(){
432        jQuery('html, body').animate({
433            scrollTop: 0
434        }, 300);
435    });
436    jQuery(document).on('removed_coupon_in_checkout', function(){
437        jQuery('html, body').animate({
438            scrollTop: 0
439        }, 300);
440    });
441
442    jQuery(document).on('divi_machine_modal_loaded', function(){
443        console.log("modal loaded");
444        setTimeout(function () {
445            console.log("modal loaded 2");
446          // for each .add_to_cart_button
447       jQuery('.post-modal-cont .et_pb_db_atc button.single_add_to_cart_button.button').each(function (i, obj) {
448         // add class ajax_add_to_cart
449         jQuery(this).addClass('ajax_add_to_cart');
450       });
451       }, 500);
452}); 
453});

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.