1jQuery(function ($) { 2 "use strict"; 3 4 $(document).on('change', '.wt_give_away_product_attr', function(){ 5 var attributes = {}; 6 var parent = $(this).closest('.wt_get_away_product'); 7 parent.find('.wt_give_away_product_attr').each(function(index) { 8 attributes[$(this).attr('data-attribute_name')] = $(this).val(); 9 }); 10 11 if(""==$(this).val()) 12 { 13 parent.find('input[name="variation_id"]').val(0); 14 parent.find('input[name="wt_variation_options"]').val(JSON.stringify( attributes )); 15 return false; 16 } 17 18 var stop_checking = false; 19 $.each( attributes, function( key, value ) { 20 if ( value == '' ) { 21 stop_checking = true; 22 return false; 23 } 24 }); 25 if( stop_checking ){ //Not every attributes selected. 26 return; 27 } 28 29 var product_id =parent.attr('product-id'); 30 31 var data = { 32 'attributes' : attributes, 33 'product' : product_id, 34 '_wpnonce' : WTSmartCouponOBJ.nonces.public 35 }; 36 37 $('.wt_choose_free_product, .wt_add_to_cart_all_btn, .checkout-button').prop('disabled', true).css({'opacity':.5, 'cursor':'not-allowed'}); 38 $('.wt_get_away_product').css({'opacity':.5, 'cursor':'wait'}); 39 jQuery.ajax({ 40 type: "POST", 41 async: true, 42 url: WTSmartCouponOBJ.wc_ajax_url + 'update_variation_id', 43 data: data, 44 dataType: 'json', 45 success:function(response) 46 { 47 $('.wt_choose_free_product, .wt_add_to_cart_all_btn, .checkout-button').prop('disabled', false).css({'opacity':1, 'cursor':'pointer'}); 48 $('.wt_get_away_product').css({'opacity':1, 'cursor':'default'}); 49 if(response.status==true) 50 { 51 parent.find('input[name="variation_id"]').val(response.variation_id); 52 parent.find('input[name="wt_variation_options"]').val(JSON.stringify( attributes )); 53 if( response.img_url && '' !== response.img_url ){ 54 parent.find( '.wt_product_image img' ).attr( 'src', response.img_url ); 55 } 56 }else{ 57 parent.find('input[name="variation_id"]').val(0); 58 59 if(false !== response.status_msg && "" !== response.status_msg.trim()) /* check message was disabled or not */ 60 { 61 alert(response.status_msg); 62 } 63 } 64 }, 65 error:function() 66 { 67 parent.find('input[name="variation_id"]').val(0); 68 $('.wt_choose_free_product, .wt_add_to_cart_all_btn, .checkout-button').prop('disabled', false).css({'opacity':1, 'cursor':'pointer'}); 69 $('.wt_get_away_product').css({'opacity':1, 'cursor':'default'}); 70 alert(WTSmartCouponOBJ.labels.error); 71 } 72 }); 73 }); 74 75 $(document).on('click', '.wt_add_to_cart_all_btn', function(e){ 76 e.preventDefault(); 77 var coupon_id=$(this).attr('coupon-id'); 78 var free_product_container=$('.wt_give_away_products[coupon="'+coupon_id+'"]'); 79 80 var variation_id_arr=new Array(); 81 var product_id_arr=new Array(); 82 var attributes_arr=new Array(); 83 var is_error=false; 84 free_product_container.find('.wt_get_away_product').each(function(){ 85 86 if($(this).attr('data-is_purchasable')==1) 87 { 88 var variation_id_obj=$(this).find('[name="variation_id"]'); 89 if(variation_id_obj.length>0) /* variable product */ 90 { 91 if(variation_id_obj.val().trim()=="" || variation_id_obj.val().trim()=="0") 92 { 93 alert(WTSmartCouponOBJ.labels.choose_variation); 94 is_error=true; 95 return false; 96 }else 97 { 98 variation_id_arr.push(variation_id_obj.val()); 99 attributes_arr.push(JSON.parse($(this).find('[name="wt_variation_options"]').val())); 100 } 101 }else{ 102 variation_id_arr.push(0); 103 attributes_arr.push([]); 104 } 105 var product_id=$(this).attr('product-id'); 106 product_id_arr.push(product_id); 107 } 108 }); 109 110 if(is_error===true) 111 { 112 return false;
113 } 114 115 var data = { 116 '_wpnonce' : WTSmartCouponOBJ.nonces.public, 117 'product_id' : 0, 118 'variation_id' : 0, 119 'attributes' : attributes_arr, 120 'coupon_id' : coupon_id, 121 'product_id_arr' : product_id_arr, 122 'variation_id_arr' : variation_id_arr, 123 'add_to_cart_all' : 1 124 }; 125 wt_sc_ajax_add_giveaway($(this), data); 126 }); 127 128 function wt_sc_ajax_add_giveaway(btn_elm, data) 129 { 130 var html_back=btn_elm.html(); 131 btn_elm.html(WTSmartCouponOBJ.labels.please_wait); 132 133 var all_btn_elms=$('.wt_add_to_cart_all_btn, .wt_choose_free_product'); 134 135 all_btn_elms.prop('disabled', true); 136 137 if ( ! $('.woocommerce-notices-wrapper').length ) { 138 $('#main').prepend( '<div class="woocommerce-notices-wrapper"></div>' ); 139 } 140 141 $('.woocommerce-notices-wrapper').html(''); 142 143 jQuery.ajax({ 144 type:"POST", 145 async:true, 146 url: WTSmartCouponOBJ.wc_ajax_url + 'wt_choose_free_product', 147 data:data, 148 success:function(response) 149 { 150 if(response=='success') 151 { 152 location.reload(); 153 }else 154 { 155 $('.woocommerce-notices-wrapper').html(response); 156 $("html, body").stop(true, true).animate({scrollTop:($('.woocommerce-notices-wrapper').offset().top-70)}, 500); 157 158 btn_elm.html(html_back); 159 all_btn_elms.prop('disabled', false); 160 } 161 }, 162 error:function() 163 { 164 btn_elm.html(html_back); 165 all_btn_elms.prop('disabled', false); 166 } 167 }); 168 } 169 170 $(document).on('click','.wt_choose_free_product',function(e) { 171 e.preventDefault(); 172 173 var parent_obj = $(this).closest('.wt_get_away_product'); 174 if(parent_obj.attr('data-is_purchasable')==1) 175 { 176 var variation_id=0; 177 var variation_id_obj=parent_obj.find('[name="variation_id"]'); 178 if(variation_id_obj.length>0) /* variable product */ 179 { 180 if(variation_id_obj.val().trim()=="" || variation_id_obj.val().trim()=="0") 181 { 182 alert(WTSmartCouponOBJ.labels.choose_variation); 183 return false; 184 }else 185 { 186 variation_id=variation_id_obj.val(); 187 } 188 } 189 190 var coupon_id = $(this).closest('.wt_give_away_products').attr('coupon'); 191 if(typeof coupon_id=='undefined') 192 { 193 return false; 194 } 195 196 var product_id = $(this).attr('prod-id'); 197 if(typeof product_id=='undefined') 198 { 199 return false; 200 } 201 202 var variation_attributes=(parent_obj.find('input[name="wt_variation_options"]').length>0 ? JSON.parse(parent_obj.find('input[name="wt_variation_options"]').val()) : ''); 203 204 var quantity=(parent_obj.find('input[name="wt_giveaway_quantity"]').length>0 ? parent_obj.find('input[name="wt_giveaway_quantity"]').val() : 0); 205 206 var data = { 207 '_wpnonce' : WTSmartCouponOBJ.nonces.public, 208 'product_id' : product_id, 209 'variation_id' : variation_id, 210 'attributes' : variation_attributes, 211 'coupon_id' : coupon_id, 212 'quantity' : quantity 213 }; 214 wt_sc_ajax_add_giveaway($(this), data); 215 } 216 217 }); 218 219});
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.