1/** 2 * BOGO public side JS file. 3 * Free product selection functionality. 4 * 5 * @since 2.0.0 6 * @package Wt_Smart_Coupon 7 */ 8 9jQuery( 10 function ($) { 11 "use strict"; 12 13 $( document ).on( 14 'change', 15 '.wbte_give_away_product_attr', 16 function () { 17 var attributes = {}; 18 var parent = $( this ).closest( '.wbte_get_away_product' ); 19 parent.find( '.wbte_give_away_product_attr' ).each( 20 function (index) { 21 attributes[$( this ).attr( 'data-attribute_name' )] = $( this ).val(); 22 } 23 ); 24 25 if ( "" == $( this ).val() ) { 26 parent.find( 'input[name="variation_id"]' ).val( 0 ); 27 parent.find( 'input[name="wt_variation_options"]' ).val( JSON.stringify( attributes ) ); 28 return false; 29 } 30 31 var stop_checking = false; 32 $.each( 33 attributes, 34 function ( key, value ) { 35 if ( '' === value ) { 36 stop_checking = true; 37 return false; 38 } 39 } 40 ); 41 if ( stop_checking ) { /** Not every attributes selected. */ 42 return; 43 } 44 45 var product_id = parent.attr( 'product-id' ); 46 47 var data = { 48 'attributes' : attributes, 49 'product' : product_id, 50 '_wpnonce' : WTSmartCouponOBJ.nonces.public 51 }; 52 53 $( '.wbte_choose_free_product, .checkout-button' ).prop( 'disabled', true ).css( {'opacity':.5, 'cursor':'not-allowed'} ); 54 $( '.wbte_get_away_product' ).css( {'opacity':.5, 'cursor':'wait'} ); 55 jQuery.ajax( 56 { 57 type: "POST", 58 async: true, 59 url: WTSmartCouponOBJ.wc_ajax_url + 'update_variation_id_on_choose', 60 data: data, 61 dataType: 'json', 62 success:function (response) { 63 $( '.wbte_choose_free_product, .checkout-button' ).prop( 'disabled', false ).css( {'opacity':1, 'cursor':'pointer'} ); 64 $( '.wbte_get_away_product' ).css( {'opacity':1, 'cursor':'default'} ); 65 if ( true === response.status ) { 66 parent.find( 'input[name="variation_id"]' ).val( response.variation_id ); 67 parent.find( 'input[name="wt_variation_options"]' ).val( JSON.stringify( attributes ) ); 68 if ( response.img_url && '' !== response.img_url ) { 69 parent.find( '.wbte_product_image img' ).attr( 'src', response.img_url ); 70 } 71 } else { 72 parent.find( 'input[name="variation_id"]' ).val( 0 ); 73 74 if ( false !== response.status_msg && "" !== response.status_msg.trim() ) { /* check message was disabled or not */ 75 alert( response.status_msg ); 76 } 77 } 78 }, 79 error:function () { 80 parent.find( 'input[name="variation_id"]' ).val( 0 ); 81 $( '.wbte_choose_free_product, .checkout-button' ).prop( 'disabled', false ).css( {'opacity':1, 'cursor':'pointer'} ); 82 $( '.wbte_get_away_product' ).css( {'opacity':1, 'cursor':'default'} ); 83 alert( WTSmartCouponOBJ.labels.error ); 84 } 85 } 86 ); 87 } 88 ); 89 90 $( document ).on( 91 'click', 92 '.wbte_choose_free_product', 93 function (e) { 94 e.preventDefault(); 95 96 var parent_obj = $( this ).closest( '.wbte_get_away_product' ); 97 if ( '1' === parent_obj.attr( 'data-is_purchasable' ) ) { 98 var variation_id = 0; 99 var variation_id_obj = parent_obj.find( '[name="variation_id"]' ); 100 if ( 0 < variation_id_obj.length ) { /* variable product */ 101 if ( "" === variation_id_obj.val().trim() || "0" === variation_id_obj.val().trim() ) { 102 alert( WTSmartCouponOBJ.labels.choose_variation ); 103 return false; 104 } else { 105 variation_id = variation_id_obj.val(); 106 } 107 } 108 109 var coupon_id = $( this ).closest( '.wbte_sc_bogo_products' ).attr( 'coupon' ); 110 if ( 'undefined' === typeof coupon_id ) { 111 return false; 112 } 113 114 var product_id = $( this ).attr( 'prod-id' ); 115 if ( 'undefined' === typeof product_id ) { 116 return false; 117 } 118 119 let free_qty = parent_obj.find( 'input[name="wbte_sc_bogo_quantity"]' ).val(); 120 free_qty = free_qty > parent_obj.attr( 'data-free-qty' ) ? parent_obj.attr( 'data-free-qty' ) : free_qty; 121 122 var variation_attributes = ( 0 < parent_obj.find( 'input[name="wt_variation_options"]' ).length ? JSON.parse( parent_obj.find( 'input[name="wt_variation_options"]' ).val() ) : '' ); 123 124 var data = { 125 '_wpnonce' : WTSmartCouponOBJ.nonces.public, 126 'product_id' : product_id, 127 'variation_id' : variation_id, 128 'attributes' : variation_attributes, 129 'coupon_id' : coupon_id, 130 'free_qty' : free_qty 131 }; 132 wbte_sc_ajax_add_giveaway( $( this ), data ); 133 } 134 135 } 136 ); 137 138 function wbte_sc_ajax_add_giveaway( btn_elm, data ) 139 { 140 var html_back = btn_elm.html(); 141 btn_elm.html( WTSmartCouponOBJ.labels.please_wait ); 142 143 var all_btn_elms = $( '.wbte_choose_free_product' ); 144 145 all_btn_elms.prop( 'disabled', true ); 146 147 if ( ! $( '.woocommerce-notices-wrapper' ).length ) { 148 $( '#main' ).prepend( '<div class="woocommerce-notices-wrapper"></div>' ); 149 } 150 151 $( '.woocommerce-notices-wrapper' ).html( '' ); 152 153 jQuery.ajax( 154 { 155 type:"POST", 156 async:true, 157 url: WTSmartCouponOBJ.wc_ajax_url + 'wbte_choose_free_product', 158 data:data, 159 success:function ( response ) { 160 if ( response ) { 161 location.reload(); 162 } else { 163 $( '.woocommerce-notices-wrapper' ).html( response ); 164 $( "html, body" ).stop( true, true ).animate( {scrollTop:( $( '.woocommerce-notices-wrapper' ).offset().top - 70 )}, 500 ); 165 166 btn_elm.html( html_back ); 167 all_btn_elms.prop( 'disabled', false ); 168 } 169 }, 170 error:function () { 171 btn_elm.html( html_back ); 172 all_btn_elms.prop( 'disabled', false ); 173 } 174 } 175 ); 176 } 177 } 178);
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.