PageSourceSearch

https://www.aerobicyfitness.com/modules/ecommaddtocart/views/js/ecommaddtocart-advanced.js

js aerobicyfitness.com collected 2026-09-29 13:22:11 UTC 12,070 bytes, 340 lines download raw bytes

1/**
2* 2007-2021 PrestaShop
3*
4* NOTICE OF LICENSE
5*
6* This source file is subject to the Academic Free License (AFL 3.0)
7* that is bundled with this package in the file LICENSE.txt.
8* It is also available through the world-wide-web at this URL:
9* http://opensource.org/licenses/afl-3.0.php
10* If you did not receive a copy of the license and are unable to
11* obtain it through the world-wide-web, please send an email
12* to [email protected] so we can send you a copy immediately.
13*
14* DISCLAIMER
15*
16* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
17* versions in the future. If you wish to customize PrestaShop for your
18* needs please refer to http://www.prestashop.com for more information.
19*
20*  @author    PrestaShop SA <[email protected]>
21*  @copyright 2007-2021 PrestaShop SA
22*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
23*  International Registered Trademark & Property of PrestaShop SA
24*
25* Don't forget to prefix your containers with your own identifier
26* to avoid any conflicts with others containers.
27*/
28/* add to cart from product list */
29// Usamos la función personalizada en vez de la nativa de Prestashop porque la nuestra tiene mensajes de error
30prestashop.blockcart.showModal = null;
31$(document).ready(function () {
32	
33	
34	$('input.p-quantity[type="number"]').on('change', function(e) {
35        if (!e.originalEvent) {
36            return; // Si fue un evento interno, no hagas nada
37        }
38        if($(this).hasClass('aw-table')) {
39        	var currentObj = $(this).closest('.mpd-input-td');
40        	var quantity = $(this).closest('.mpd-input-td').find('input.p-quantity[type="number"]').val();
41        } else {
42        	var currentObj = $(this).closest('.ecommaddtocart_productlist');
43        	var quantity = $(this).closest('.ecommaddtocart_productlist').find('input.p-quantity[type="number"]').val();
44        }
45        
46        var id_product = currentObj.attr('attr-id-product');
47    	var id_combination = currentObj.attr('attr-id_product_attribute');
48        
49        if(quantity > 0 && quantity != '') {
50        	var cart_unit = 0;
51        	if (typeof prestashop.cart !== 'undefined') {
52				$.each(prestashop.cart.products, function(index, producto) {
53					
54				  // Acceder a los campos deseados para cada producto
55				  var idProducto = producto.id;
56				  var idProductoAttribute = producto.id_product_attribute;
57				  var unidades = producto.cart_quantity;
58				 
59				   if(idProducto == id_product && id_combination == idProductoAttribute) {
60			      		cart_unit = unidades; 
61			      }
62			  
63				  
64				});
65			}
66        	// setTimeout(function(){
67       	 	quantity = quantity - cart_unit;
68       	 	ecomm_add_to_cart_form(currentObj, quantity)
69       	 	// }, 2500);
70        }
71       
72       
73    });
74	
75	
76    var showModal = "";
77    prestashop.on(
78        'updateCart',
79        function (event) {
80             ecomm_add_to_cart_refresh(event);
81        }
82    );
83
84
85    $(document).on('click', '.add-to-cart-productlist', function (e) {
86        var currentObj = $(this).closest('.ecommaddtocart_productlist');
87        ecomm_add_to_cart_changebutton(currentObj, "number", 1);
88        //actualizamos el valor total superior
89        if ($('.blockcart .cart-products-count').length){
90            var currentqty = parseInt($('.blockcart .cart-products-count').text());
91            currentqty = currentqty + 1;
92            $('.blockcart .cart-products-count').text(currentqty);
93        }
94    });
95
96    $(document).on('click', '.plus-prod', function (e) {
97        var currentQty = parseInt($(this).parent().find('.p-quantity').val())+1;
98        $(this).parent().find('.p-quantity').val(currentQty);
99        ecomm_add_to_cart_form($(e.target).closest('.ecommaddtocart_productlist'));
100    })
101    $(document).on('click', '.minus-prod', function (e) {
102        if (parseInt($(this).parent().find('.p-quantity').val()) > 1) {
103            var currentQty = parseInt($(this).parent().find('.p-quantity').val())-1;
104            $(this).parent().find('.p-quantity').val(currentQty);
105            ecomm_add_to_cart_form($(e.target).closest('.ecommaddtocart_productlist'), -1);
106        } else {
107            $(this).parent().find('.p-quantity').val(0);
108            var currentObj = $(this).closest('.ecommaddtocart_productlist');
109            ecomm_add_to_cart_changebutton(currentObj, "button", 0);
110            ecomm_add_to_cart_form ($(e.target).closest('.ecommaddtocart_productlist'), 0);
111          }
112    })
113
114    /* Add and substract quantity input controls */
115    $(document).on('click', '.product-miniature .add_cart_content .bootstrap-touchspin-up', function (e) {
116        e.preventDefault();
117        var currQty = $(this).closest('.input-group').find('input[name="qty"]').val();
118        $(this).closest('.input-group').find('input[name="qty"]').val(parseInt(currQty) + 1);
119    });
120    $(document).on('click', '.product-miniature .add_cart_content .bootstrap-touchspin-down', function (e) {
121        e.preventDefault();
122        var currQty = $(this).closest('.input-group').find('input[name="qty"]').val();
123        if (currQty > 1) {
124            $(this).closest('.input-group').find('input[name="qty"]').val(parseInt(currQty) - 1);
125        }
126    });
127});
128
129function ecomm_add_to_cart_changebutton(obj, type = "button", qty = 1){
130    if (type == "number"){
131        obj.find('.add-to-cart-productlist').addClass("hide");
132        obj.find('.product-quantity').addClass("show");
133        obj.find('.product-quantity').find('.p-quantity').val(qty);
134    }
135    else {
136        obj.find('.add-to-cart-productlist').removeClass("hide");
137        obj.find('.product-quantity').removeClass("show");
138        obj.find('.product-quantity').find('.p-quantity').val(qty);
139    }
140}
141
142function ecomm_add_to_cart_refresh(event){
143	    if(event.resp.cart != undefined) {
144		    	event.resp.cart.products.forEach( function(value, index, array) {
145		        var id_product = value.id_product;
146		        var currentObj = $(".ecommaddtocart_productlist input[value='"+id_product+"']").closest('.ecommaddtocart_productlist');
147		        ecomm_add_to_cart_changebutton(currentObj, "number", value.cart_quantity);
148	    	});
149	    }
150		
151	
152    
153}
154function ecomm_add_to_cart_form(el, qty) {
155console.log(el)
156    var id_product = el.attr('attr-id-product');
157    var id_combination = el.attr('attr-id_product_attribute');
158    var id_customization = false;
159    var groups = {};
160    var form_action  = false;
161    var extra_var
162    // Añadir al carro
163    ecomm_add_to_cart(id_product, qty, id_customization, groups, form_action, extra_var, id_combination)
164}
165function xecomm_add_to_cart_form($form, extra_var, qty_form = undefined) {
166    // Inicialización por defecto en el caso que no nos pasen estas variables
167    if (typeof (extra_var) == 'undefined' || !Object.keys(extra_var).length) {
168        var extra_var = {};
169    }
170
171    // Serializar el formulario
172    var serialized_form = $form.serializeArray();
173
174    // Lo pasamos a array asociativo
175    var array_form = {};
176    $.each(serialized_form, function () {
177        array_form[this.name] = this.value;
178    });
179
180    // Extraer del form para para la función
181    var id_product = undefined;
182    var qty = undefined;
183    var id_customization = undefined;
184    var groups = {};
185    $.each(array_form, function (name, val) {
186        switch (name) {
187            case "id_product":
188                id_product = val;
189                break;
190            case "qty":
191                qty = val;
192                break;
193            case "id_customization":
194                id_customization = val;
195                break;
196            default:
197                groups[name] = val;
198        }
199    });
200    var form_action = $form.attr('action');
201
202    if (qty_form != undefined)
203        qty = qty_form;
204
205
206
207    // Añadir al carro
208    ecomm_add_to_cart(id_product, qty, id_customization, groups, form_action, extra_var);
209}
210
211
212/* *
213 * Añadir un producto al carrito por Ajax
214 *
215 * + id_producto -> Id del productoa añadir. Es obligatoria.
216 *
217 * - qty -> Cantidad que se quiere añadir al carro. Por defecto 1.
218 * - id_customization -> Id de la personalización concreta del producto. Por defecto 0.
219 * - groups -> Array asociativo con todos los campos extra que necesitemos enviar, por ejemplo los atributos seleccionados. Por defecto vacío.
220 * - form_action -> A que URL debe hacerse la petición. Por defecto al carrito
221 * - extra_var -> Array asociativo de valores extra a pasar por get
222 *
223 */
224function ecomm_add_to_cart(id_product, qty, id_customization, groups, form_action, extra_var, id_combination) {
225	$('.error_text_addtocart').remove();
226	if (typeof (qty) == 'undefined') {
227        var qty = 1;
228    }
229    //actualizamos el valor total superior
230    if ($('.blockcart .cart-products-count').length){
231        var currentqty = parseInt($('.blockcart .cart-products-count').text());
232        if (qty < 0) {
233            currentqty = currentqty - 1;
234        }
235        else if(qty == 0){
236            currentqty = currentqty - 1;
237        }
238        else{
239            currentqty = currentqty + parseInt(qty);
240        }
241        currentqty = parseInt(currentqty);
242        $('.blockcart .cart-products-count').text(currentqty);
243    }
244
245
246    // Inicialización por defecto en el caso que no nos pasen estas variables
247
248    if (typeof (id_customization) == 'undefined' || id_customization == false) {
249        var id_customization = 0;
250    }
251
252
253    if (typeof (groups) == 'undefined' || !Object.keys(groups).length) {
254        var groups = [];
255    }
256    if (typeof (form_action) == 'undefined' || form_action == false) {
257        var form_action = prestashop.urls.pages.cart;
258    }
259    if (typeof (extra_var) == 'undefined' || !Object.keys(extra_var).length) {
260        var extra_var = {};
261    }
262
263    // Pasar la información a formato array asociativo
264    var form_data = {};
265    form_data['token'] = prestashop.static_token;
266    form_data['id_product'] = id_product;
267
268    form_data['id_customization'] = id_customization;
269
270    if (Number.isInteger(id_combination)) {
271        form_data['id_product_attribute'] = id_combination;
272    }
273
274    $.each(groups, function (name, value) {
275        form_data[name] = value;
276    });
277
278
279    extra_var['action'] = 'update';
280    if (qty < 0) {
281        extra_var['add'] = 1;
282        form_data['qty'] = qty;
283        extra_var['op'] = 'down';
284    }
285    else if(qty == 0){
286        extra_var['delete'] = '1';
287    }
288    else{
289        form_data['qty'] = qty;
290        extra_var['add'] = 1;
291        extra_var['op'] = 'up';
292    }
293
294
295    extra_var['ajax'] = 1;
296
297    // Montamos la url en formato get para la petición post
298    //var query = $.param(form_data) + '&' + $.param(extra_var);
299    var data_final = $.extend(form_data, extra_var);
300 
301
302    // Petición post para añadir al carro por ajaz
303    $.ajax({
304        type: "POST",
305        url: form_action,
306        data: data_final,
307        success: function (resp) {
308            if (resp.hasError) {
309                show_modal(resp.errors, 'error');
310            } else {
311                var resp = jQuery.parseJSON(resp);
312                prestashop.emit('updateCart', {
313                    reason: {
314                        idProduct: resp.id_product,
315                        idProductAttribute: resp.id_product_attribute,
316                        idCustomization: resp.id_customization,
317                        linkAction: 'add-to-cart',
318                        cart: resp.cart
319                    },
320                    resp: resp
321                });
322                console.log(resp);
323                if (resp.hasError == true) {
324		          //showModal(resp.modal);
325		          var spanElement = $('<span class="error_text_addtocart">').text(resp.errors);
326    			  // Agregar el span al principio del contenedor
327    			  if($('#awtableproduct').length) {
328    			  	$('#awtableproduct').prepend(spanElement);
329    			  }
330				  
331		        }
332            }
333        },
334        error: function (resp, status, error) {
335            prestashop.emit('handleError', {eventType: 'addProductToCart', resp: resp});
336        }
337    });
338
339
340}

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.