Subversion Repositories SmartDukaan

Rev

Rev 14313 | Rev 14357 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14313 Rev 14345
Line 8... Line 8...
8
		});
8
		});
9
	}
9
	}
10
	if($('.storeminprice').length>0) {
10
	if($('.storeminprice').length>0) {
11
		var globalminprice = 9999999;
11
		var globalminprice = 9999999;
12
		var globalminsku;		
12
		var globalminsku;		
-
 
13
		var calls = [];
13
		$('.storeminprice').each(function(){
14
		$('.storeminprice').each(function(){
-
 
15
			var temp = {'sort':$(this).data('searchorder'),'val':$(this)};
-
 
16
			calls.push(temp);
-
 
17
		});
-
 
18
		calls = sortByKey(calls,'sort');		
-
 
19
		for(var i in calls){
-
 
20
			console.log(calls[i]['val']);
-
 
21
			fetchLivePrice(calls[i]['val'])
-
 
22
		}
-
 
23
	};
-
 
24
	function sortByKey(array, key) {
-
 
25
	    return array.sort(function(a, b) {
-
 
26
	        var x = a[key]; var y = b[key];
-
 
27
	        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
-
 
28
	    });
-
 
29
	};
-
 
30
	function fetchLivePrice(obj){
-
 
31
		console.log(obj);
14
			var inStock = false;
32
		var inStock = false;
15
			var that = $(this);			
33
		var that = obj;			
16
			$.ajax({
34
		$.ajax({
17
				url: '/store_products/getliveprice/'+$(this).data('bundle_id')+'/'+$(this).data('id'),
35
			url: '/store_products/getliveprice/'+$(that).data('bundle_id')+'/'+$(that).data('id'),
18
				// Tell jQuery we're expecting JSONP
36
			// Tell jQuery we're expecting JSONP
19
				dataType: "json",
37
			dataType: "json",
20
				// Tell YQL what we want and that we want JSON			
38
			// Tell YQL what we want and that we want JSON			
21
				method: 'get',
39
			method: 'get',
-
 
40
			timeout: 180000,
22
				// Work with the response
41
			// Work with the response
23
				success: function( response ) {
42
			success: function( response ) {
24
					if(response.success){
43
				if(response.success){
25
						var i = 0;	
44
					var i = 0;	
26
						var minpriceindex = 0;				
45
					var minpriceindex = 0;				
27
						if(response.products[i].in_stock == 1){inStock = true;}
46
					if(response.products[i].in_stock == 1){inStock = true;}
28
						var minprice = response.products[0]['available_price'];
47
					var minprice = response.products[0]['available_price'];
29
						var minpriceurl = response.products[0]['marketPlaceUrl'];
48
					var minpriceurl = response.products[0]['marketPlaceUrl'];
30
						if(response.products.length>0){
49
					if(response.products.length>0){
31
							//More than one products in store
50
						//More than one products in store
32
							var variants = [];
51
						var variants = [];
33
							// var i = 0;
52
						// var i = 0;
34
							for(var i in response.products){	
53
						for(var i in response.products){	
35
								if(response.products[i].in_stock == 1){inStock = true;}						
54
							if(response.products[i].in_stock == 1){inStock = true;}						
36
								console.log(response.products[i]);
55
							console.log(response.products[i]);
37
								if(response.products[i].available_price && response.products[i].available_price < globalminprice) {
56
							if(response.products[i].available_price && response.products[i].available_price < globalminprice) {
38
									globalminprice = response.products[i].available_price;
57
								globalminprice = response.products[i].available_price;
39
									globalminsku = response.products[i]._id;
58
								globalminsku = response.products[i]._id;
40
								}
-
 
41
								if(response.products[i].available_price <= minprice) {
-
 
42
									minprice = response.products[i].available_price;
-
 
43
									minpriceindex = i;
-
 
44
								} 
-
 
45
								console.log(response);
-
 
46
								variants.push({'available_price' : response.products[i].available_price, 'url' : response.products[i].marketPlaceUrl,'source_product_name' : response.products[i].source_product_name,'id' : response.products[i]._id});
-
 
47
							}
-
 
48
							if(i>0){
-
 
49
								var variantslink = $('<span class="variants">+ '+i+' Options</span>');
-
 
50
								$(that).parent().append(variantslink);							
-
 
51
								$(variantslink).data('variants',variants)
-
 
52
							}
59
							}
-
 
60
							if(response.products[i].available_price <= minprice) {
-
 
61
								minprice = response.products[i].available_price;
-
 
62
								minpriceindex = i;
-
 
63
							} 
-
 
64
							console.log(response);
-
 
65
							variants.push({'available_price' : response.products[i].available_price, 'url' : response.products[i].marketPlaceUrl,'source_product_name' : response.products[i].source_product_name,'id' : response.products[i]._id});
53
						}
66
						}
54
						if(inStock){
67
						if(i>0){
55
							$(that).html(minprice);
-
 
56
							$('#sku-'+$(that).data('id')).data('id',response.products[minpriceindex]['_id']).removeClass('hidden');
-
 
57
							console.log(globalminprice+' '+globalminsku);
-
 
58
							if(globalminprice != 9999999) {
-
 
59
								$('#bestprice').html(globalminprice);
-
 
60
								$('#beststorelink').data('id',globalminsku).removeClass('hidden');
68
							var variantslink = $('<span class="variants">+ '+i+' Options</span>');
61
								$('#bestpricecontainer').removeClass('hidden');
69
							$(that).parent().append(variantslink);							
62
							}
-
 
63
						}else{
-
 
64
							$(that).parent().parent().remove();	
70
							$(variantslink).data('variants',variants)
65
						}
71
						}
-
 
72
					}
-
 
73
					if(inStock){
-
 
74
						$(that).html(minprice);
-
 
75
						$('#sku-'+$(that).data('id')).data('id',response.products[minpriceindex]['_id']).removeClass('hidden');
-
 
76
						console.log(globalminprice+' '+globalminsku);
-
 
77
						if(globalminprice != 9999999) {
66
						if(!response.products[0].available_price){
78
							$('#bestprice').html(globalminprice);
-
 
79
							$('#beststorelink').data('id',globalminsku).removeClass('hidden');
67
							$(that).parent().parent().remove();	
80
							$('#bestpricecontainer').removeClass('hidden');
68
						}
81
						}
69
					}else{
82
					}else{
-
 
83
						$(that).parent().parent().remove();	
-
 
84
					}
-
 
85
					if(!response.products[0].available_price){
70
						$(that).parent().parent().remove();
86
						$(that).parent().parent().remove();	
71
					}
87
					}
72
				},
88
				}else{
73
				error : function() {
-
 
74
					$(that).parent().parent().remove();
89
					$(that).parent().parent().remove();
75
				}
90
				}
76
			});
91
			},
-
 
92
			error : function() {
-
 
93
				$(that).parent().parent().remove();
-
 
94
			}
77
		});
95
		});
78
	}
96
	}
79
	$(document).on('click','.variants',function(){
97
	$(document).on('click','.variants',function(){
80
		$('.storeproductinfo').empty();
98
		$('.storeproductinfo').empty();
81
		var variants = $(this).data('variants');	
99
		var variants = $(this).data('variants');	
Line 130... Line 148...
130
	});
148
	});
131
	// $('.row').on('click','.viewproduct',function(e){
149
	// $('.row').on('click','.viewproduct',function(e){
132
	$(document).on('click','.viewproduct',function(e){
150
	$(document).on('click','.viewproduct',function(e){
133
		$('#loadingModal').modal();
151
		$('#loadingModal').modal();
134
		console.log($(this).data('id'));
152
		console.log($(this).data('id'));
135
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id');
153
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id')+'/'+$(this).data('source')+'/?url='+encodeURIComponent($(this).data('url'))+'&price='+$(this).data('price');
136
		$.ajax({
154
		$.ajax({
137
			url: url,
155
			url: url,
138
			// The name of the callback parameter, as specified by the YQL service
156
			// The name of the callback parameter, as specified by the YQL service
139
			jsonp: "callback",
157
			jsonp: "callback",
140
			// Tell jQuery we're expecting JSONP
158
			// Tell jQuery we're expecting JSONP