PageSourceSearch

https://babybasic.co.il/wp-content/themes/baby/js/category-add-to-cart.js?ver=1.0.4

js babybasic.co.il collected 2026-10-08 00:45:47 UTC 12,859 bytes, 286 lines download raw bytes

1jQuery(function($) {
2    var variation_button;
3    var ajaxCartData = {};
4                    
5    function triggerFreeShippingOverlay() {
6        $.ajax({
7            url: localize.ajax_url,
8            method: 'GET',
9            dataType: 'json',
10            data: {
11                action: 'get_min_amount_via_ajax',
12            },
13            success: function(result) {
14                var minAmount = parseInt(result.min_amount);
15                var total = parseInt(result.total);
16                // debugger;
17                $('#cart-summary-overlay').addClass('active');
18                if (minAmount <= total) {
19                    $('#cart-summary-overlay .cart-summery-free-shipping-unlocked').addClass('active');
20                    $('#cart-summary-overlay .cart-summery-free-shipping').removeClass('active');
21
22                } else {
23                    $('#cart-summary-overlay .cart-summery-free-shipping-unlocked').removeClass('active');
24                    $('#cart-summary-overlay .cart-summery-free-shipping').addClass('active');
25                    $('#amount-left-for-freeshiping').html(minAmount - total);
26                    $('#amount-progress-bar').css('width', total / minAmount * 100 + '%');
27                }
28
29                variation_button.parents('li.product').removeClass('loading');
30            },
31            error: function(jqXHR, textStatus, errorThrown) {
32                console.log(jqXHR, textStatus, errorThrown);
33            }
34        });
35    }
36
37    function add_to_cart_ajax(data) {
38        $.ajax({
39            url: localize.ajax_url,
40            method: 'POST',
41            dataType: 'json',
42            data: {
43                action: 'add_to_cart_via_ajax',
44                form_data: data,
45            },
46            success: function(result) {
47                variation_button.attr('data-key', result.is_added);
48
49                var mini_cart_wrapper = $('.mini-cart-wrap');
50
51                if (result.cart_total_count >= 0) {
52                    cart_total_count = result.cart_total_count;
53                }
54
55                $('.menu-item-cart-count').html(cart_total_count);
56                $('.menu-item-cart-count').click();
57
58                if (result.mini_cart_html != '') {
59                    mini_cart_wrapper.find('.mini-cart').replaceWith(result.mini_cart_html);
60                }
61
62                mini_cart_wrapper.find('.quantity').find('input[type="number"].qty').bootstrapNumber();
63
64                mini_cart_wrapper.find('.quantity').animate({
65                    opacity: 1
66                }, 300);
67
68                mini_cart_wrapper.removeClass('ajax-loading');
69
70                // variation_button.removeClass('add_variant_to_cart');
71                variation_button.addClass('remove_variant_from_cart');
72
73                variation_button.siblings().removeClass('active');
74                //variation_button.addClass('active');
75                $('.add_variant_to_cart').removeClass('active');
76                triggerFreeShippingOverlay();
77            },
78            error: function(jqXHR, textStatus, errorThrown) {
79                console.log(jqXHR, textStatus, errorThrown);
80            }
81        });
82    }
83
84    function add_to_cart_multiple_ajax(data) {
85        $.ajax({
86            url: localize.ajax_url,
87            method: 'POST',
88            dataType: 'json',
89            data: {
90                action: 'add_to_cart_via_ajax',
91                form_data: data,
92            },
93            success: function(result) {
94                // since this item has multiple attributes we need to mark it's parent instead of the given button
95                //variation_button.closest('.size-popup-varients').addClass('in-cart');
96                variation_button.closest('.size-popup-varients').attr('data-key', result.is_added);
97                variation_button.attr('data-key', result.is_added);
98                var mini_cart_wrapper = $('.mini-cart-wrap');
99                if (result.cart_total_count >= 0) {
100                    cart_total_count = result.cart_total_count;
101                }
102                $('.menu-item-cart-count').html(cart_total_count);
103                $('.menu-item-cart-count').click();
104                
105                if (result.mini_cart_html != '') {
106                    mini_cart_wrapper.find('.mini-cart').replaceWith(result.mini_cart_html);
107                }
108                mini_cart_wrapper.find('.quantity').find('input[type="number"].qty').bootstrapNumber();
109                mini_cart_wrapper.find('.quantity').animate({
110                    opacity: 1
111                }, 300);
112                mini_cart_wrapper.removeClass('ajax-loading');
113                // variation_button.removeClass('add_variant_to_cart');
114                variation_button.addClass('remove_variant_from_cart');
115                variation_button.siblings().removeClass('active');
116                //variation_button.addClass('active');
117                $('.add_variant_to_cart').removeClass('active');
118                triggerFreeShippingOverlay();
119            },
120            error: function(jqXHR, textStatus, errorThrown) {
121                console.log(jqXHR, textStatus, errorThrown);
122            }
123        });
124    }
125    
126    function remove_from_cart_ajax(data) {
127        variation_button.parents('li.product').addClass('loading');
128        $.ajax({
129            url: localize.ajax_url,
130            method: 'POST',
131            dataType: 'json',
132            data: {
133                action: 'remove_from_cart_via_ajax',
134                form_data: data,
135            },
136            success: function(result) {
137                var mini_cart_wrapper = $('.mini-cart-wrap');
138                if (result.cart_total_count >= 0) {
139                    cart_total_count = result.cart_total_count;
140                }
141
142                $('.menu-item-cart-count').html(cart_total_count);
143                $('.menu-item-cart-count').click();
144
145                if (result.mini_cart_html != '') {
146                    mini_cart_wrapper.find('.mini-cart').replaceWith(result.mini_cart_html);
147                }
148
149                mini_cart_wrapper.find('.quantity').find('input[type="number"].qty').bootstrapNumber();
150                mini_cart_wrapper.find('.quantity').animate({
151                    opacity: 1
152                }, 300);
153
154                variation_button.parents('li.product').removeClass('loading');
155                mini_cart_wrapper.removeClass('ajax-loading');
156                variation_button.removeClass('remove_variant_from_cart');
157                variation_button.removeClass('active');
158                variation_button.removeAttr('data-key');
159                // handlers for multiple attributes
160                variation_button.closest('.size-popup-varients').removeAttr('data-key');
161                variation_button.closest('.size-popup-varients').removeClass('in-cart');
162                triggerFreeShippingOverlay();
163            },
164            error: function(jqXHR, textStatus, errorThrown) {
165                console.log(jqXHR, textStatus, errorThrown);
166            }
167        });
168    }
169
170    $('body').on('click', '.woocommerce-mini-cart-item a.remove_from_cart_button', function(e) {
171        console.log('clicked');
172        var cart_item_key = $(this).attr('data-cart_item_key');
173        console.log(cart_item_key);
174        $('.size-popup-varients-list').find("[data-key='" + cart_item_key + "']").removeClass('active');
175        $('.size-popup-varients-list').find("[data-key='" + cart_item_key + "']").removeClass('remove_variant_from_cart');
176        
177        $("[data-key='" + cart_item_key + "']").find('.add_variant_to_cart').removeClass('active');
178        $("[data-key='" + cart_item_key + "']").find('.add_variant_to_cart').removeClass('remove_variant_from_cart');
179        $("[data-key='" + cart_item_key + "']").removeAttr('data-key'); 
180    });
181                                                        
182    $(document).ready(function() {
183
184        $('.add_variant_to_cart').on('click', function(ev) {
185            // debugger;
186            ev.preventDefault();
187            ev.stopPropagation();
188            if ($(this).parents('li.product').hasClass('loading')) {
189                return;
190            }
191
192            variation_button = $(this);
193
194            if(variation_button.find('span').length) return false; // disable click if product is out of stock
195            
196            // how many attributes we have?
197            var attributes_count = variation_button.closest('.size-popup-varients').find('.size-popup-varients-list').length;
198            
199            // normal add to cart functionality when there's only 1 attribute or not variable product
200            if(attributes_count < 2) {
201                // if (variation_button.parents('li.product').length > 0) {
202                variation_button.parents('li.product').addClass('loading');
203
204                /*
205                if (variation_button.hasClass('remove_variant_from_cart')) {
206                    var key = variation_button.attr('data-key');
207                    var data = {
208                        key: key
209                        }
210                        // debugger;
211                    $(this).parents('.sizes-popup-wrapper').find('.error').removeClass('active');
212
213                    remove_from_cart_ajax(data);
214                } else {
215                */
216                    $(this).parents('.sizes-popup-wrapper').find('.error').removeClass('active');
217
218                    var data = {
219                        product_id: $(this).attr('data-id'),
220                        variation_id: $(this).attr('data-variation-id'),
221                    };
222
223                    var attributeName = $(this).data('attribute_name');
224                    var attributeValue = $(this).data('attribute_value');
225                    data[attributeName] = attributeValue;
226                            
227                    var activeElement = $(this).siblings('.active');
228                    var k = activeElement.attr('data-key');
229                    if (k) {
230                        console.log('remove and then add');
231                    //    data['key'] = k;                    
232                    }
233                    add_to_cart_ajax(data);
234                /*
235                }
236                */
237            } else {
238                if($(this).hasClass('active')) {
239                    $(this).removeClass('active');
240                    // when product is in the cart we have to remove it first
241                    if(variation_button.closest('.size-popup-varients').hasClass('in-cart')) {
242                        var key = variation_button.closest('.size-popup-varients').attr('data-key');
243                        var data = {
244                            key: key
245                        };
246                        $(this).parents('.sizes-popup-wrapper').find('.error').removeClass('active');
247                        remove_from_cart_ajax(data);                
248                    } 
249    
250                } else {
251                    variation_button.closest('.size-popup-varients-list').find('.add_variant_to_cart').removeClass('active');
252                    
253                    $(this).addClass('active');
254                    ajaxCartData['product_id'] = $(this).data('id');
255                    // check if each attribute was selected
256                    var selected_attributes_count = 0;
257                    variation_button.closest('.size-popup-varients').find('.size-popup-varients-list').each(function(){
258                        // add to cart only one item is selected 
259                        if($(this).find('.active').length == 1) {
260                            var attributeName = $(this).find('.active').data('attribute_name');
261                            var attributeValue = $(this).find('.active').data('attribute_value');
262                            ajaxCartData[attributeName] = attributeValue;
263                            selected_attributes_count++;
264                        } 
265                    });
266
267                    // trigger remove when other variation previously was added
268                    if(variation_button.closest('.size-popup-varients').hasClass('in-cart')) {
269                        var key = variation_button.closest('.size-popup-varients').attr('data-key');
270                        if (key) {
271                            console.log('remove and then add');
272                            ajaxCartData['key'] = key;                    
273                        }               
274                    } 
275                    
276                    //add to cart when all attributes were selected
277                    if(attributes_count == selected_attributes_count) {
278                        variation_button.parents('li.product').addClass('loading');
279                        add_to_cart_multiple_ajax(ajaxCartData);
280                    }
281                }
282            }
283        });
284
285    });
286});

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.