PageSourceSearch

https://www.twonav.com/js/w4cmm/ventascruzadas/ventas_cruzadas.js?q=22166

js twonav.com collected 2026-10-01 13:04:49 UTC 18,231 bytes, 560 lines download raw bytes

1/*
2	W4CMM_Ventas_Cruzadas
3*/
4var VentasCruzadas = {
5	// Debug mode
6	debug: false,
7	_debugString: '',
8
9	// Parámetros de configuración del módulo
10	urlLastItem: '',
11	urlDeleteItem: '',
12	moduleEnabled: false,
13	removeCartEnabled: false,
14	sumEnabled: false,
15
16	// Required parameters to initialize with init function	
17	priceOfProduct: 0,
18	currencySymbol: null,
19
20	// Other parameters
21	productSku: 0,
22	productId: 0,
23
24	// Texts
25	textAdding: "Adding...",
26	textAdded: "Added",
27	textAdd: "Add",
28	textRemove: "Remove",
29	loadingText: "Loading...",
30	
31	// Ajax config
32	ajaxType: 'GET',
33
34	// Classes and IDs config
35	addToCartClass: 'addToCart',
36	addedToCartClass: 'is-disabled',
37	classCrossellDelete: 'crossell_delete',
38	smCartProClass: 'oneitem-total-price .oneitem-price',
39
40	slider : null,
41	//Custom LightCheckout
42	/* 	No permite integrarlo, está constantemente en busca de su HTML,
43		cosa que no tenemos en entorno de catalog_product_view
44	checkout: null,
45	lightCheckoutUrl : '', 
46  	lightCheckoutSaveOrderUrl : '',
47	lightCheckoutBillingTaxvatEnabled : '',
48	*/
49
50	// Inicializar
51	init: function(options){
52		if(options){
53			for(var key in options){
54				this[key] = options[key];
55			}
56		}
57		//this.initCustomLightCheckout();
58	},
59
60	setOption : function(key, value) {
61		if(this[key]) {
62			this[key] = value;
63		}
64	},
65
66	/* 	No permite integrarlo, está constantemente en busca de su HTML,
67		cosa que no tenemos en entorno de catalog_product_view
68	initCustomLightCheckout: function(){
69		this.checkout = new Lightcheckout({ 
70			url : this.lightCheckoutUrl,
71			save_order_url : this.lightCheckoutSaveOrderUrl,	
72			billing_taxvat_enabled : this.lightCheckoutBillingTaxvatEnabled
73		});	
74	},*/
75
76	showConfig: function(){
77		if(!this.debug) {
78			return;
79		}
80		console.log('urlLastItem: '+this.urlLastItem);
81		console.log('urlDeleteItem: '+this.urlDeleteItem);
82		console.log('moduleEnabled: '+this.moduleEnabled);
83		console.log('sumEnabled: '+this.sumEnabled);
84		console.log('removeCartEnabled: '+this.removeCartEnabled);
85		console.log('moduleEnabled: '+this.moduleEnabled);
86		console.log('debug: '+this.debug);
87		console.log('priceOfProduct: '+this.priceOfProduct);
88		console.log('productSku: '+this.productSku);
89		console.log('productId: '+this.productId);
90		console.log('currencySymbol: '+this.currencySymbol);
91		console.log('textAdding: '+this.textAdding);
92		console.log('textAdded: '+this.textAdded);
93		console.log('textAdd: '+this.textAdd);
94		console.log('loadingText: '+this.loadingText);
95		console.log('ajaxType: '+this.ajaxType);
96		console.log('addToCartClass: '+this.addToCartClass);
97		console.log('addedToCartClass: '+this.addedToCartClass);
98		/*console.log('-- CHECKOUT --');
99		console.log('lightCheckoutUrl: '+this.lightCheckoutUrl);
100		console.log('lightCheckoutSaveOrderUrl: '+this.lightCheckoutSaveOrderUrl);
101		console.log('lightCheckoutBillingTaxvatEnabled: '+this.lightCheckoutBillingTaxvatEnabled);*/
102	},
103
104	getDebug: function(){
105		if(!this.debug) {
106			return;
107		}
108
109		if(this._debugString.length > 0) {
110			console.log('\n---------------------------------------------------\n');
111			console.log(this._debugString);
112			console.log('---------------------------------------------------\n');
113		}
114	},
115
116	resetLog: function(){
117		this._debugString = '';
118	},
119
120	log: function(msg){
121		this._debugString += msg;
122	},
123
124	handleClick: function(productThis){
125		if(jQuery(productThis).hasClass(this.addToCartClass) && jQuery(productThis).attr('disabled') === undefined || jQuery(productThis).attr('disabled') === false) {
126			VentasCruzadas.resetLog();
127
128			productId = jQuery(productThis).data('product-id');
129			product = jQuery("a."+this.addToCartClass+"[data-product-id='"+productId+"']");
130			this._addToCart(product);	
131		}
132		
133		return false;
134	},
135
136	_addToCart: function(productElement){
137		product = productElement;
138		this.productSku = product.data('sku');
139		this.productId = product.data('product-id');
140		this._disableAllButtons();
141		var url = encodeURI(product.data('urladd'));
142        productElement  .removeClass("btnFlat")
143                        .removeClass("accent")
144                        .removeClass(VentasCruzadas.addToCartClass)
145                        .addClass(VentasCruzadas.addedToCartClass)
146		                .html(this.textAdding);
147		this._showLoading();
148		this._pauseSlider();		
149
150		jQuery.ajax({
151		   	type: VentasCruzadas.ajaxType,
152		  	url: url,
153		   	success: function(){
154		   		var $product = product.closest(".js-addtocart-element");
155
156                $product.addClass("is-active");
157
158		     	productElement.html(VentasCruzadas.textAdded);
159
160	     		VentasCruzadas._recalculatePrice(product);
161	     		VentasCruzadas._showRemoveButton(productElement);
162
163	     		VentasCruzadas._enableAllButtons();
164	     		VentasCruzadas.getDebug();
165
166	     		W4CMM_MiniCartScroll.getCartQty();
167	     		VentasCruzadas._startSlider();
168
169                // jQuery('.oneitem-product:first').before(jQuery('.oneitem-product:last-child').clone().addClass('cross-added').addClass('cross-id-' + jQuery(product).attr('data-product-id')));
170                // jQuery('.cross-added').find('.oneitem-total-price').remove();
171                //
172                //
173                // var crossProductImg = $product.find('.js-recommended-product-image').attr('src');
174                // var crossProductName = jQuery.trim($product.find('.js-recommended-product-name').text());
175                // var crossProductPrice = jQuery.trim($product.find('.js-recommended-product-current-price').text());
176                // var crossProductoldPrice = jQuery.trim($product.find('.js-recommended-product-old-price').text());
177                //
178                // var txt_oldprice = "";
179                // if (crossProductoldPrice!=crossProductPrice) {
180                // 	txt_oldprice = "<span class=\"price\"><span class=\"old-price\">" + crossProductoldPrice + "</span></span>&nbsp;";
181                // }
182                // crossProductPrice = "<span class=\"price\">" + crossProductPrice + "</span>";
183                // var crossProductSku = $product.find('.producto_recomendado_addtocart .btnFlat').attr('data-sku');
184                // var crossProductId = jQuery(product).attr('data-product-id');
185                //
186                // var $elem = jQuery('.cross-id-' + crossProductId);
187                // var crossProductItemId = $product.find('.icon-delete').attr('data-item-id');
188                // var crossProductLink = $product.attr('data-link');
189                // var deleteLinkSplitted = $elem.find('.js-item-product-remove').attr('href').split('/id/');
190                // var deleteLink = deleteLinkSplitted[0] + '/id/' + crossProductItemId + '/';
191                //
192                // $elem.find('.js-item-product-img').attr('src', crossProductImg);
193                // $elem.find('.js-item-product-name').attr('title', crossProductName).attr('item_sku', crossProductSku).attr('item_id', crossProductId);
194                // $elem.find('.js-item-product-name').html(crossProductName + ' <span class="js-item-product-quantity">(x1)</span>');
195                // $elem.find('.js-item-product-price').html(txt_oldprice + crossProductPrice);
196                // $elem.find('.js-item-product-remove').attr('href', deleteLink);
197
198	     		VentasCruzadas._hideLoading();
199			},
200			error: function(){
201                productElement  .addClass("btnFlat")
202                                .addClass("accent")
203                                .addClass(VentasCruzadas.addToCartClass)
204                                .removeClass(VentasCruzadas.addedToCartClass);
205				productElement.html(VentasCruzadas.textAdd);
206
207				VentasCruzadas._enableAllButtons();
208				VentasCruzadas.getDebug();
209				VentasCruzadas._startSlider();
210				VentasCruzadas._hideLoading();
211			}
212		});	
213	},
214
215	// Disable all available buttons that have VentasCruzadas.addToCartClass
216	_disableAllButtons : function() {
217		jQuery('.'+VentasCruzadas.addToCartClass).attr('disabled', 'disabled');
218		if(jQuery('.'+VentasCruzadas.classCrossellDelete).length > 0) {
219			jQuery('.'+VentasCruzadas.classCrossellDelete).attr('disabled', 'disabled');
220		}
221	},
222
223	// Enable all available buttons that have VentasCruzadas.addToCartClass
224	_enableAllButtons : function() {
225		jQuery('.'+VentasCruzadas.addToCartClass).removeAttr('disabled');
226		jQuery( '.'+VentasCruzadas.addedToCartClass).removeAttr('disabled');
227		if(jQuery('.'+VentasCruzadas.classCrossellDelete).length > 0) {
228			jQuery('.'+VentasCruzadas.classCrossellDelete).removeAttr('disabled');
229		}
230	},
231
232	_isSumEnabled : function() {
233		if(this._isModuleEnabled() && this.sumEnabled == 1) {
234			return true;
235		}
236		return false;
237	},
238
239	_isModuleEnabled : function() {
240		if(this.moduleEnabled == 1) {
241			return true;
242		}
243		return false;
244	},
245
246	_isRemoveCartEnabled : function() {
247		if(this._isModuleEnabled() && this.removeCartEnabled == 1) {
248			return true;
249		}
250		return false;
251	},
252
253	_recalculatePrice : function(product, operation) {		
254		if(!this._isSumEnabled()) {
255			this.log('SUM NOT ENABLED\n');
256			return;
257		}
258
259		operation = typeof operation !== 'undefined' ? operation : 'add';
260
261		if (operation == 'add') {
262			this._addPrice(product);
263		} else if (operation == 'substract') {
264			this._substactPrice(product);
265		}
266	},
267
268	_addPrice : function(product) {
269		finalPrice = parseFloat(product.data('final-price'));
270		oldPrice = parseFloat(product.data('old-price'));
271
272		this.log('>>>>>>>>> ADD >>>>>>>>>\n');
273		this.log('Price of product: '+ this.priceOfProduct + '\n');
274     	this.log('Product data-final-price: '+ finalPrice + '\n');
275
276     	// Suma de precios al añadir un crossell
277     	this.priceOfProduct = this.priceOfProduct + finalPrice;
278
279     	this.log('Resultado de sumar: '+this.priceOfProduct + '\n');
280
281     	/*priceToShow = this._getFormattedPrice(this.priceOfProduct); */
282
283		//Suma de precios al añadir un crossell
284		priceToShow = this._numberFormat(this.priceOfProduct);
285		priceToShow = priceToShow.replace(",00", "");
286     	jQuery('.'+this.smCartProClass).html(priceToShow + ' ' + this.currencySymbol);
287
288     	this.log('priceToShow: '+priceToShow + '\n');
289     	this.log('Clase a cambiar: .'+this.smCartProClass + '\n');
290     	this.log('>>>>>>>>>  END  >>>>>>>>>\n');
291     },
292
293     _substactPrice : function() {
294     	finalPrice = parseFloat(product.data('final-price'));
295		oldPrice = parseFloat(product.data('old-price'));
296
297     	this.log('<<<<<<<<< SUBSTRACT <<<<<<<<<\n');
298     	this.log('Price of product: '+ this.priceOfProduct + '\n');
299     	this.log('Product data-final-price: '+ finalPrice + '\n');
300
301     	// Resta de precios al eliminar un crossell del carrito
302     	this.priceOfProduct = this.priceOfProduct - finalPrice;
303
304     	this.log('Resultado de restar: '+this.priceOfProduct + '\n');
305
306		// Resta de precios al eliminar un crossell del carrito
307		if(this.priceOfProduct <= 0) {
308			this.priceOfProduct = 0;
309		}
310		priceToShow = this._numberFormat(this.priceOfProduct);
311		priceToShow = priceToShow.replace(",00", "");
312     	jQuery('.'+this.smCartProClass).html(priceToShow + ' ' + this.currencySymbol);
313
314     	this.log('priceToShow: '+priceToShow + '\n');
315     	this.log('Clase a cambiar: .'+this.smCartProClass+ '\n');
316     	this.log('<<<<<<<<<     END     <<<<<<<<<\n');
317    },
318
319	_showRemoveButton : function(product) {
320		if(!this._isRemoveCartEnabled()) {
321			this.log('REMOVE CART NOT ENABLED\n');
322			return;
323		}
324
325		lastItemId = jQuery.ajax({
326		   	type: 'GET',
327		  	url: VentasCruzadas.urlLastItem,
328		  	global: false,
329		  	async: false,
330		   	success: function(data){
331		   		return data;
332			},
333			error: function(data){
334				return 0;
335			}
336		}).responseText;	
337
338		this.log('lastItemId: '+lastItemId+'\n');
339		htmlButton = '<a class="icon-delete '+this.classCrossellDelete+'" data-product-id="'+this.productId+'" data-sku="'+this.productSku+'" data-item-id="'+lastItemId+'" href="#" onClick="VentasCruzadas.remove(this); return false;" title="'+this.textRemove+'"></a>';
340		product.parent().append(htmlButton);
341		this.log('Añadido botón\n');
342	},
343
344	_showLoading : function(time) {
345		show_loading(this.loadingText, time);
346	},
347
348	_hideLoading : function() {
349		hide_loading();
350	},
351
352	_pauseSlider : function() {
353		/*if(this.slider !== 'undefined') {
354			this.slider.stopAuto();
355		}*/
356	},
357
358	_startSlider : function() {
359		/*if(this.slider !== 'undefined') {
360			this.slider.startAuto();
361		}*/		
362	},
363
364	_hideRemoveButton : function(element) {
365		removeElement = element.parent().find('a.'+this.classCrossellDelete);
366		console.log(removeElement);
367		removeElement.remove();
368		this.log('Eliminado botón\n');	
369	},
370
371	_showDefaultButton : function(product) {
372		product.html(this.textAdd);
373     	product.addClass(this.addToCartClass).removeClass(this.addedToCartClass);
374     	this.log('Button set as default\n');
375	},
376
377	/*
378	<a class="icon-delete crossell_delete" data-product-id="197218" 
379	data-sku="1075_33000C2" data-item-id="278710" href="#" 
380	onclick="VentasCruzadas.remove(this)" title="Quitar este artículo"></a>
381	*/
382	remove : function(thisElement) {
383		if(jQuery(thisElement).attr('disabled') === undefined || jQuery(thisElement).attr('disabled') === false) {
384			this._showLoading();
385			this._pauseSlider();
386			this._disableAllButtons();
387			element = jQuery(thisElement);
388			sku = element.data('sku');
389			productId = element.data('product-id');
390			itemId = element.data('item-id');
391			elements = jQuery("a."+this.addedToCartClass+"[data-product-id='"+productId+"']");
392
393			deletedSuccessfully = jQuery.ajax({
394			   	type: 'POST',
395			   	data: {/*itemId : itemId*/ productId : productId},
396			  	url: VentasCruzadas.urlDeleteItem,
397			  	global: false,
398			  	async: false,
399			   	success: function(data){
400			   		return data;
401				},
402				error: function(data){
403					return false;
404				}
405			}).responseText;
406
407			this.log('deletedSuccessfully: '+deletedSuccessfully+'\n');
408
409			deleted = false;
410			if(deletedSuccessfully == '1') {
411				deleted = true;
412				this.log('Eliminado del carrito el producto con ITEM ID: '+itemId+' y SKU: '+sku+'\n');		
413			} else {
414				this.log('No se ha podido eliminar el producto del carrito\n');
415			}
416
417			W4CMM_MiniCartScroll.getCartQty();
418			
419			if(deleted) {
420				product = element.parent().find('a[data-sku="'+sku+'"]');
421				this._recalculatePrice(product, 'substract');
422				this.log('Restado precio al producto\n');
423
424                jQuery('.cross-id-' + jQuery(thisElement).attr('data-product-id')).remove();
425
426				this._hideRemoveButton(elements);
427				this._showDefaultButton(elements);
428			}
429			this._enableAllButtons();
430			VentasCruzadas._hideLoading();
431			VentasCruzadas._startSlider();
432		} else {
433			this.log('El botón está deshabilitado\n');
434		}
435
436		this.getDebug();
437	},
438
439	_getFormattedPrice: function(price){
440		price.toFixed(2).replace(/,/g, function(c, i, a) {
441		    return i && c !== "." && ((a.length - i) % 3 === 0) ? ',' + c : c;
442		});
443	},
444
445	_numberFormat : function(number) { //, decimals, dec_point, thousands_sep) {
446		decimals = 2;
447		dec_point = ',';
448		thousands_sep = ' ';
449	    var n = !isFinite(+number) ? 0 : +number, 
450	        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
451	        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
452	        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
453	        toFixedFix = function (n, prec) {
454	            // Fix for IE parseFloat(0.55).toFixed(0) = 0;
455	            var k = Math.pow(10, prec);
456	            return Math.round(n * k) / k;
457	        },
458	        s = (prec ? toFixedFix(n, prec) : Math.round(n)).toString().split('.');
459	    if (s[0].length > 3) {
460	        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
461	    }
462	    if ((s[1] || '').length < prec) {
463	        s[1] = s[1] || '';
464	        s[1] += new Array(prec - s[1].length + 1).join('0');
465	    }
466	    return s.join(dec);
467	},
468
469	_bindFancyboxClose : function() {
470		jQuery('a.fancybox-close').on('click', function(e) {
471			e.preventDefault();
472			if(jQuery('.addedToCart').length > 0) {
473				location.reload();
474			}
475		});
476	},
477
478	removeAjax : function(element) {
479		var $this = this;
480		var $elem = jQuery(element);
481
482		var $top_container = $elem.closest(".cross-added");
483
484		urlDelete = $elem.attr('href');
485		this._showLoading();
486
487		jQuery.ajax({
488		   	type: 'GET',
489		  	url: urlDelete,
490		   	success: function(data){
491		   		if (jQuery('.js-item-products .oneitem-product').length === 1) {
492		   			location.reload();
493				} else {
494		   			var splittedClass = '';
495
496		   			if($top_container.length) {
497                        splittedClass = $top_container.attr('class').split('cross-id-');
498					}
499
500					if (splittedClass) {
501						//Producto añadido vía cross-selling
502						var procutId = splittedClass[1];
503						jQuery(jQuery('.icon-delete[data-product-id="' + procutId + '"]')[0]).click();
504
505						jQuery(".js-addtocart-element[item_id='" + procutId + "']").removeClass("is-active");
506
507					} else {
508					    //Producto original
509                        // var price = parseFloat($elem.parent().parent().parent().find('.oneitem-product .js-item-product-price .price').text());
510                        // var cartPrice = parseFloat(jQuery('.' + $this.smCartProClass).text());
511                        // var priceToShow = $this._numberFormat(cartPrice - price).replace(",00", "");
512
513						$elem.closest(".oneitem-product").remove();
514						// jQuery('.' + $this.smCartProClass).html(priceToShow + ' ' + $this.currencySymbol);
515                    }
516				}
517		   		VentasCruzadas._hideLoading();
518			},
519			error: function(data){
520				VentasCruzadas._hideLoading();
521			}
522		});
523	},
524};
525
526jQuery(document).ready(function(){
527	//TODO: Idk if this is used anymore...
528	jQuery(".fancyboxVentasCruzadas").fancybox({
529	    scrolling : 'no',
530		//minHeight : '500',
531		width: '100%',		
532		autoScale : false, //true,
533		autoSize: true,
534		mouseWheel: false,
535		fitToView: false, 
536		margin: 0,
537		closeClick : false, // prevents closing when clicking INSIDE fancybox
538		openEffect : 'none',
539		hideOnOverlayClick : false,
540		beforeClose: function() {
541			hideAllQtips();
542
543			if (jQuery('#mapEndSelection').length) {
544				jQuery('#mapEndSelection').attr('disabled', 'disabled');
545				jQuery('#selection').hide();
546			}
547		},
548		helpers : {
549			title : {
550				type : false
551			},
552			overlay : {
553				css : {
554					'background' : 'rgba(238,238,238,0.85)'
555				},
556				closeClick: false // prevents closing when clicking OUTSIDE fancybox
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.