1/** 2* 3* JavaScript file that has public action in dokan dashboard 4* 5*/ 6(function($){ 7 8 "use strict"; 9 10 $( document ).ready(function() { 11 12 /* Offers */ 13 // Initialize DataTables 14 var mainTable; 15 if (typeof $('#b2bkingdokan_dashboard_offers_table').DataTable === "function") { 16 mainTable = $('#b2bkingdokan_dashboard_offers_table').DataTable(); 17 } 18 19 // when page opens, check if quote is set (response to make offer) 20 let params = (new URL(document.location)).searchParams; 21 let quote = params.get('quote'); // is the string "Jonathan Smith". 22 if (quote !== null && quote !== ''){ 23 // we have a number 24 let quotenr = parseInt(quote); 25 setTimeout(function(){ 26 $('.b2bking_dokan_new_offer').click(); 27 }, 100); 28 29 // get values via AJAX and load into edit 30 // first run ajax call based on the offer id 31 var datavar = { 32 action: 'b2bking_get_offer_data', 33 security: b2bkingdokan_display_settings.security, 34 quoteid: quotenr 35 }; 36 37 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 38 var results = response; 39 var resultsArray = results.split('*'); 40 // load values into fields 41 $('#b2bking_admin_offer_textarea').val(resultsArray[2]); 42 $('#b2bking_category_users_textarea').val(resultsArray[0]); 43 44 offerRetrieveHiddenField(); 45 offerCalculateTotals(); 46 }); 47 48 49 50 } 51 52 // When New Offer modalzz is opened 53 $('body').on('click', '.b2bking_dokan_new_offer', openOffermodalzz); 54 function openOffermodalzz(){ 55 clearOfferValues(); 56 $('.b2bking_dokan_save_new_offer').val('new'); 57 setTimeout(function(){ 58 $('.b2bking_offer_product_selector').select2(); 59 }, 200); 60 } 61 62 // Delete offer 63 $('body').on('click', '.b2bking_offer_delete_table', function(){ 64 let offer = $(this).val(); 65 if (confirm(b2bkingdokan_display_settings.are_you_sure_delete_offer)){ 66 var datavar = { 67 action: 'b2bking_delete_ajax_offer', 68 security: b2bkingdokan_display_settings.security, 69 offerid: offer, 70 userid: $('#b2bking_new_offer_user_id').val() 71 }; 72 73 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 74 location.reload(); 75 }); 76 } 77 }); 78 79 function clearOfferValues(){ 80 $('.b2bking_dokan_email_offer').remove(); 81 $('.b2bking_group_visibility_container_content_checkbox_input').prop('checked',false); 82 $('#b2bking_category_users_textarea').val(''); 83 $('#b2bking_offer_customtext_textarea').val(''); 84 $('#b2bking_new_offer_title').val(''); 85 $('.b2bking_offer_line_number').each(function(){ 86 // remove all except first 87 if ($(this).attr('ID') !== 'b2bking_offer_number_1'){ 88 $(this).remove(); 89 } 90 // clear first 91 $('#b2bking_offer_number_1 .b2bking_offer_text_input').val(''); 92 $('#b2bking_offer_number_1 .b2bking_offer_product_selector').val('').trigger('change'); 93 offerCalculateTotals(); 94 offerSetHiddenField(); 95 }); 96 } 97 98 // Email Offer 99 $('body').on('click', '.b2bking_dokan_email_offer', function(){ 100 let offeridd = $(this).val(); 101 102 if (confirm(b2bkingdokan_display_settings.email_offer_confirm)){ 103 var datavar = { 104 action: 'b2bking_email_offer_dokan', 105 security: b2bkingdokan_display_settings.security, 106 offerid: offeridd, 107 offerlink: b2bkingdokan_display_settings.offers_endpoint_link, 108 }; 109 110 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 111 112 alert(b2bkingdokan_display_settings.email_has_been_sent); 113 }); 114 } 115 }); 116 117 // Edit Offer 118 $('body').on('click', '.b2bking_offer_edit_table', function (){ 119 var offer_id = $(this).val(); 120 setTimeout(function(){ 121 // clear all values 122 clearOfferValues();
123 124 // set button for save offer 125 $('.b2bking_dokan_save_new_offer').val(offer_id); 126 127 // add email offer button 128 $('.b2bking_dokan_save_new_offer').after('<button type="button" value="'+offer_id+'" class="dokan-btn dokan-btn-theme b2bking_dokan_email_offer">'+b2bkingdokan_display_settings.email_offer+'</button>'); 129 130 }, 200); 131 // get values via AJAX and load into edit 132 // first run ajax call based on the offer id 133 var datavar = { 134 action: 'b2bking_get_offer_data', 135 security: b2bkingdokan_display_settings.security, 136 offerid: offer_id, 137 userid: $('#b2bking_new_offer_user_id').val() 138 }; 139 140 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 141 var results = response; 142 var resultsArray = results.split('*'); 143 // load values into fields 144 $('#b2bking_offer_customtext_textarea').val(resultsArray[3]); 145 $('#b2bking_admin_offer_textarea').val(resultsArray[2]); 146 $('#b2bking_category_users_textarea').val(resultsArray[0]); 147 $('#b2bking_new_offer_title').val(resultsArray[4]); 148 // foreach group visible 149 let groups = resultsArray[1].split(','); 150 groups.forEach((element) => { 151 $('#'+element).prop('checked', true); 152 }); 153 offerRetrieveHiddenField(); 154 offerCalculateTotals(); 155 }); 156 }); 157 158 // Save Offers 159 $('.b2bking_dokan_save_new_offer').on('click', function(){ 160 var vall = $(this).val(); 161 if (!$('#b2bking_new_offer_title').val()){ 162 alert(b2bkingdokan_display_settings.offer_must_have_title); 163 return; 164 } 165 if (!$('#b2bking_admin_offer_textarea').val()){ 166 alert(b2bkingdokan_display_settings.offer_must_have_product); 167 return; 168 } 169 170 if (confirm(b2bkingdokan_display_settings.are_you_sure_save_offer)){ 171 var datavar = { 172 action: 'b2bking_save_new_ajax_offer', 173 security: b2bkingdokan_display_settings.security, 174 uservisibility: $('#b2bking_category_users_textarea').val(), 175 customtext: $('#b2bking_offer_customtext_textarea').val(), 176 offerdetails: $('#b2bking_admin_offer_textarea').val(), 177 userid: $('#b2bking_new_offer_user_id').val(), 178 offertitle: $('#b2bking_new_offer_title').val(), 179 newedit: $('.b2bking_dokan_save_new_offer').val() 180 }; 181 182 // send quote 183 let quote = params.get('quote'); // is the string "Jonathan Smith". 184 if (quote !== null && quote !== ''){ 185 datavar.b2bking_quote_response = quote; 186 } 187 188 // b2bking_group_visibility_container_content 189 // for each checkbox adde 190 var groupvisibilitytext = ''; 191 $('.b2bking_group_visibility_container_content_checkbox_input:checkbox:checked').each(function(){ 192 groupvisibilitytext += $(this).attr('name')+','; 193 }); 194 195 datavar.groupvisibility = groupvisibilitytext; 196 197 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 198 var offeridd = response; 199 // ask if email the offer 200 if (vall === 'new'){ 201 if (confirm(b2bkingdokan_display_settings.also_email_offer)){ 202 var datavar = { 203 action: 'b2bking_email_offer_dokan', 204 security: b2bkingdokan_display_settings.security, 205 offerid: offeridd, 206 offerlink: b2bkingdokan_display_settings.offers_endpoint_link, 207 }; 208 209 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 210 alert(b2bkingdokan_display_settings.email_has_been_sent); 211 window.location=b2bkingdokan_display_settings.offers_link; 212 }); 213 } else { 214 window.location=b2bkingdokan_display_settings.offers_link; 215 } 216 } else { 217 window.location=b2bkingdokan_display_settings.offers_link; 218 } 219 220 }); 221 } 222 }); 223 224 // When click "add item" add new offer item 225 $('body').on('click', '.b2bking_offer_add_item_button', addNewOfferItem); 226 227 var offerItemsCounter = 1;
228 function addNewOfferItem(){ 229 // destroy select2 230 $('.b2bking_offer_product_selector').select2(); 231 $('.b2bking_offer_product_selector').select2('destroy'); 232 233 let currentItem = offerItemsCounter; 234 let nextItem = currentItem+1; 235 offerItemsCounter++; 236 $('#b2bking_offer_number_1').clone().attr('id', 'b2bking_offer_number_'+nextItem).insertAfter('#b2bking_offer_number_1'); 237 // clear values from clone 238 $('#b2bking_offer_number_'+nextItem+' .b2bking_offer_text_input').val(''); 239 $('#b2bking_offer_number_'+nextItem+' .b2bking_offer_product_selector').val('').trigger('change'); 240 // remove delete if it exists 241 $('#b2bking_offer_number_'+nextItem+' .b2bking_offer_delete_item_button').remove(); 242 243 $('#b2bking_offer_number_'+nextItem+' .b2bking_item_subtotal').text(b2bkingdokan_display_settings.currency_symbol+'0'); 244 // add delete button to new item 245 $('<button type="button" class="secondary-button button b2bking_offer_delete_item_button">'+b2bkingdokan_display_settings.text_delete+'</button>').insertAfter('#b2bking_offer_number_'+nextItem+' .b2bking_offer_add_item_button'); 246 247 //reinitialize select2 248 $('.b2bking_offer_product_selector').select2(); 249 } 250 251 // On click "delete" 252 $('body').on('click', '.b2bking_offer_delete_item_button', function(){ 253 $(this).parent().parent().remove(); 254 offerCalculateTotals(); 255 offerSetHiddenField(); 256 }); 257 258 // On quantity or price change, calculate totals 259 $('body').on('input', '.b2bking_offer_item_quantity, .b2bking_offer_item_name, .b2bking_offer_item_price', function(){ 260 offerCalculateTotals(); 261 offerSetHiddenField(); 262 }); 263 264 function offerCalculateTotals(){ 265 let total = 0; 266 // foreach item calculate subtotal 267 $('.b2bking_offer_item_quantity').each(function(){ 268 let quantity = $(this).val(); 269 let price = $(this).parent().parent().find('.b2bking_offer_item_price').val(); 270 if (quantity !== undefined && price !== undefined){ 271 // set subtotal 272 total+=price*quantity; 273 $(this).parent().parent().find('.b2bking_item_subtotal').text(b2bkingdokan_display_settings.currency_symbol+Number((price*quantity).toFixed(4))); 274 } 275 }); 276 277 // finished, add up subtotals to get total 278 $('#b2bking_offer_total_text_number').text(b2bkingdokan_display_settings.currency_symbol+Number((total).toFixed(4))); 279 } 280 281 function offerSetHiddenField(){ 282 let field = ''; 283 // clear textarea 284 $('#b2bking_admin_offer_textarea').val(''); 285 // go through all items and list them IF they have PRICE AND QUANTITY 286 $('.b2bking_offer_item_quantity').each(function(){ 287 let quantity = $(this).val(); 288 let price = $(this).parent().parent().find('.b2bking_offer_item_price').val(); 289 if (quantity !== undefined && price !== undefined && quantity !== null && price !== null && quantity !== '' && price !== ''){ 290 // Add it to string 291 let name = $(this).parent().parent().find('.b2bking_offer_item_name').val(); 292 if (name === undefined || name === ''){ 293 name = '(no title)'; 294 } 295 field+= name+';'+quantity+';'+price+'|'; 296 } 297 }); 298 299 // at the end, remove last character 300 field = field.substring(0, field.length - 1); 301 $('#b2bking_admin_offer_textarea').val(field); 302 } 303 304 function offerRetrieveHiddenField(){ 305 // get field; 306 let field = $('#b2bking_admin_offer_textarea').val(); 307 let itemsArray = field.split('|'); 308 // foreach condition, add condition, add new item 309 itemsArray.forEach(function(item){ 310 let itemDetails = item.split(';'); 311 if (itemDetails[0] !== undefined && itemDetails[0] !== ''){
312 $('#b2bking_offer_number_'+offerItemsCounter+' .b2bking_offer_item_name').val(itemDetails[0]); 313 $('#b2bking_offer_number_'+offerItemsCounter+' .b2bking_offer_item_quantity').val(itemDetails[1]); 314 $('#b2bking_offer_number_'+offerItemsCounter+' .b2bking_offer_item_price').val(itemDetails[2]); 315 addNewOfferItem(); 316 } 317 }); 318 // at the end, remove the last Item added 319 if (offerItemsCounter > 1){ 320 $('#b2bking_offer_number_'+offerItemsCounter).remove(); 321 } 322 323 } 324 325 // on clicking "add tier" in the product dashboard page 326 $('body').on('click', '.b2bking_product_add_tier', function() { 327 var groupid = $(this).parent().find('.b2bking_groupid').val(); 328 $('<div class="dokan-form-group dokan-clearfix dokan-price-container show_if_simple"><div class="content-half-part"><div class="dokan-input-group"><span class="dokan-input-group-addon"><></span><input type="text" class="b2bking_tiered_pricing_element dokan-product-regular-price wc_input_price dokan-form-control" name="b2bking_group_'+groupid+'_pricetiers_quantity[]" placeholder="'+b2bkingdokan_display_settings.min_quantity_text+'"></div></div><div class="content-half-part sale-price"><div class="dokan-input-group"><span class="dokan-input-group-addon">'+b2bkingdokan_display_settings.currency_symbol+'</span><input type="text" class="b2bking_tiered_pricing_element dokan-product-sales-price wc_input_price dokan-form-control" name="b2bking_group_'+groupid+'_pricetiers_price[]" placeholder="'+b2bkingdokan_display_settings.final_price_text+'"></div></div></div>').insertBefore($(this).parent()); 329 }); 330 331 // on clicking "add tier" for variations in the product dashboard page 332 $('body').on('click', '.b2bking_product_add_tier_variation', function() { 333 var groupid = $(this).parent().find('.b2bking_groupid').val(); 334 var variationid = $(this).parent().find('.b2bking_variationid').val(); 335 $('<div class="dokan-form-group dokan-clearfix dokan-price-container show_if_simple"><div class="content-half-part"><div class="dokan-input-group"><span class="dokan-input-group-addon"><></span><input type="text" class="b2bking_tiered_pricing_element dokan-product-regular-price wc_input_price dokan-form-control" name="b2bking_group_'+groupid+'_'+variationid+'_pricetiers_quantity[]" placeholder="'+b2bkingdokan_display_settings.min_quantity_text+'"></div></div><div class="content-half-part sale-price"><div class="dokan-input-group"><span class="dokan-input-group-addon">'+b2bkingdokan_display_settings.currency_symbol+'</span><input type="text" class="b2bking_tiered_pricing_element dokan-product-sales-price wc_input_price dokan-form-control" name="b2bking_group_'+groupid+'_'+variationid+'_pricetiers_price[]" placeholder="'+b2bkingdokan_display_settings.final_price_text+'"></div></div></div>').insertBefore($(this).parent()); 336 }); 337 338 // on clicking "add row" in the product dashboard page 339 $('body').on('click', '.b2bking_product_add_row', function() { 340 var groupid = $(this).parent().find('.b2bking_groupid').val(); 341 $('<div class="dokan-form-group dokan-clearfix dokan-price-container show_if_simple"><div class="content-half-part"><div class="dokan-input-group"><span class="dokan-input-group-addon">'+b2bkingdokan_display_settings.label_text+'</span><input type="text" class="b2bking_customrow_element dokan-form-control" name="b2bking_group_'+groupid+'_customrows_label[]"></div></div><div class="content-half-part sale-price"><div class="dokan-input-group"><span class="dokan-input-group-addon">'+b2bkingdokan_display_settings.text_text+'</span><input type="text" class="b2bking_customrow_element dokan-form-control" name="b2bking_group_'+groupid+'_customrows_text[]" ></div></div></div>').insertBefore($(this).parent()); 342 }); 343 344 /* Product Visibility */ 345 // On page load, update product visibility options 346 updateProductVisibilityOptions(); 347 348 // On Product Visibility option change, update product visibility options 349 $('#b2bking_product_visibility_override').change(function() { 350 updateProductVisibilityOptions(); 351 }); 352 353 // Checks the selected Product Visibility option and hides or shows Automatic / Manual visibility options 354 function updateProductVisibilityOptions(){ 355 let selectedValue = $("#b2bking_product_visibility_override").children("option:selected").val(); 356 if(selectedValue === "manual") { 357 $("#b2bking_metabox_product_categories_wrapper").css("display","none"); 358 $("#b2bking_product_visibility_override_options_wrapper").css("display","block"); 359 } else if (selectedValue === "default"){ 360 $("#b2bking_product_visibility_override_options_wrapper").css("display","none"); 361 $("#b2bking_metabox_product_categories_wrapper").css("display","block"); 362 } 363 } 364 365 // On clicking the "Add user button in the Product Category User Visibility table" 366 $("#b2bking_category_add_user").on("click",function(){ 367 // Get username 368 let username = $("#b2bking_all_users_dropdown").children("option:selected").text(); 369 // Get content and check if username already exists
370 let content = $("#b2bking_category_users_textarea").val(); 371 let usersarray = content.split(','); 372 let exists = 0; 373 374 $.each( usersarray, function( i, val ) { 375 if (val.trim() === username){ 376 exists = 1; 377 } 378 }); 379 380 if (exists === 1){ 381 // Show "Username already in the list" for 3 seconds 382 $("#b2bking_category_add_user").text(b2bkingdokan_display_settings.username_already_list); 383 setTimeout(function(){ 384 $("#b2bking_category_add_user").text(b2bkingdokan_display_settings.add_user); 385 }, 2000); 386 387 } else { 388 // remove last comma and whitespace after 389 content = content.replace(/,\s*$/, ""); 390 // if list is not empty, add comma 391 if (content.length > 0){ 392 content = content + ', '; 393 } 394 // add username 395 content = content + username; 396 $("#b2bking_category_users_textarea").val(content); 397 } 398 }); 399 400 /* Dynamic Rules */ 401 402 // Initialize Select2s 403 $('#b2bking_rule_select_who').select2(); 404 $('#b2bking_rule_select_applies').select2(); 405 406 407 // initialize multiple products / categories selector as Select2 408 $('.b2bking_select_multiple_product_categories_selector_select, .b2bking_select_multiple_users_selector_select').select2({'width':'100%', 'theme':'classic'}); 409 // show hide multiple products categories selector 410 showHideMultipleProductsCategoriesSelector(); 411 $('#b2bking_rule_select_what').change(showHideMultipleProductsCategoriesSelector); 412 $('#b2bking_rule_select_applies').change(showHideMultipleProductsCategoriesSelector); 413 function showHideMultipleProductsCategoriesSelector(){ 414 let selectedValue = $('#b2bking_rule_select_applies').val(); 415 let selectedWhat = $('#b2bking_rule_select_what').val(); 416 if (selectedValue === 'multiple_options' && selectedWhat !== 'tax_exemption_user'){ 417 $('#b2bking_select_multiple_product_categories_selector').css('display','block'); 418 } else { 419 $('#b2bking_select_multiple_product_categories_selector').css('display','none'); 420 } 421 } 422 423 showHideMultipleUsersSelector(); 424 $('#b2bking_rule_select_who').change(showHideMultipleUsersSelector); 425 function showHideMultipleUsersSelector(){ 426 let selectedValue = $('#b2bking_rule_select_who').val(); 427 if (selectedValue === 'multiple_options'){ 428 $('#b2bking_select_multiple_users_selector').css('display','block'); 429 } else { 430 $('#b2bking_select_multiple_users_selector').css('display','none'); 431 } 432 } 433 434 function setUpConditionsFromHidden(){ 435 // get all conditions 436 let conditions = $('#b2bking_rule_select_conditions').val(); 437 if (conditions === undefined) { 438 conditions = ''; 439 } 440 441 if(conditions.trim() !== ''){ 442 let conditionsArray = conditions.split('|'); 443 let i=1; 444 // foreach condition, create selectors 445 conditionsArray.forEach(function(item){ 446 let conditionDetails = item.split(';'); 447 // if condition not empty 448 if (conditionDetails[0] !== ''){ 449 $('.b2bking_dynamic_rule_condition_name.b2bking_condition_identifier_'+i).val(conditionDetails[0]); 450 $('.b2bking_dynamic_rule_condition_operator.b2bking_condition_identifier_'+i).val(conditionDetails[1]); 451 $('.b2bking_dynamic_rule_condition_number.b2bking_condition_identifier_'+i).val(conditionDetails[2]); 452 addNewCondition(i, 'programatically'); 453 i++; 454 } 455 }); 456 } 457 } 458 459 // On clicking "add condition" in Dynamic rule 460 $('body').on('click', '.b2bking_dynamic_rule_condition_add_button', function(event) { 461 addNewCondition(1,'user'); 462 }); 463
464 function addNewCondition(buttonNumber = 1, type = 'user'){ 465 let currentNumber; 466 let nextNumber; 467 468 // If condition was added by user 469 if (type === 'user'){ 470 // get its current number 471 let classList = $('.b2bking_dynamic_rule_condition_add_button').attr('class').split(/\s+/); 472 $.each(classList, function(index, item) { 473 if (item.includes('identifier')) { 474 var itemArray = item.split("_"); 475 currentNumber = parseInt(itemArray[3]); 476 } 477 }); 478 // set next number 479 nextNumber = (currentNumber+1); 480 } else { 481 // If condition was added at page load automatically 482 currentNumber = buttonNumber; 483 nextNumber = currentNumber+1; 484 } 485 486 // add delete button same condition 487 $('.b2bking_dynamic_rule_condition_add_button.b2bking_condition_identifier_'+currentNumber).after('<button type="button" class="b2bking_dynamic_rule_condition_delete_button b2bking_condition_identifier_'+currentNumber+'">'+b2bkingdokan_display_settings.delete+'</button>'); 488 // add next condition 489 $('#b2bking_condition_number_'+currentNumber).after('<div id="b2bking_condition_number_'+nextNumber+'" class="b2bking_rule_condition_container">'+ 490 '<select class="b2bking_dynamic_rule_condition_name b2bking_condition_identifier_'+nextNumber+'">'+ 491 '<option value="product_quantity">'+b2bkingdokan_display_settings.product_quantity+'</option>'+ 492 '<option value="product_value">'+b2bkingdokan_display_settings.product_value+'</option>'+ 493 '</select>'+ 494 '<select class="b2bking_dynamic_rule_condition_operator b2bking_condition_identifier_'+nextNumber+'">'+ 495 '<option value="greater">'+b2bkingdokan_display_settings.greater+'</option>'+ 496 '<option value="equal">'+b2bkingdokan_display_settings.equal+'</option>'+ 497 '<option value="smaller">'+b2bkingdokan_display_settings.smaller+'</option>'+ 498 '</select>'+ 499 '<input type="number" step="0.00001" class="b2bking_dynamic_rule_condition_number b2bking_condition_identifier_'+nextNumber+'" placeholder="'+b2bkingdokan_display_settings.enter_quantity_value+'">'+ 500 '<button type="button" class="b2bking_dynamic_rule_condition_add_button b2bking_condition_identifier_'+nextNumber+'">'+b2bkingdokan_display_settings.add_condition+'</button>'+ 501 '</div>'); 502 503 // remove self 504 $('.b2bking_dynamic_rule_condition_add_button.b2bking_condition_identifier_'+currentNumber).remove(); 505 506 // update available options 507 updateDynamicRulesOptionsConditions(); 508 } 509 510 // On clicking "delete condition" in Dynamic rule 511 $('body').on('click', '.b2bking_dynamic_rule_condition_delete_button', function () { 512 // get its current number 513 let currentNumber; 514 let classList = $(this).attr('class').split(/\s+/); 515 $.each(classList, function(index, item) { 516 if (item.includes('identifier')) { 517 var itemArray = item.split("_"); 518 currentNumber = parseInt(itemArray[3]); 519 } 520 }); 521 // remove current element 522 $('#b2bking_condition_number_'+currentNumber).remove(); 523 524 // update conditions hidden field 525 updateConditionsHiddenField(); 526 }); 527 528 // On Rule selector change, update dynamic rule conditions 529 $('#b2bking_rule_select_what, #b2bking_rule_select_who, #b2bking_rule_select_applies, #b2bking_rule_select, #b2bking_rule_select_showtax, #b2bking_container_tax_shipping').change(function() { 530 updateDynamicRulesOptionsConditions(); 531 }); 532 533 function updateDynamicRulesOptionsConditions(){ 534 $('#b2bking_rule_select_applies_replaced_container').css('display','none'); 535 // Hide one-time fee 536 $('#b2bking_one_time').css('display','none'); 537 // Hide all condition options 538 $('.b2bking_dynamic_rule_condition_name option').css('display','none'); 539 // Hide quantity/value 540 $('#b2bking_container_quantity_value').css('display','none'); 541 // Hide currency
542 $('#b2bking_container_currency').css('display','none'); 543 // Hide payment methods 544 $('#b2bking_container_paymentmethods').css('display','none'); 545 // Hide countries and requires 546 $('#b2bking_container_countries, #b2bking_container_requires, #b2bking_container_showtax').css('display','none'); 547 // Hide tax name 548 $('#b2bking_container_taxname, #b2bking_container_tax_shipping, #b2bking_container_tax_shipping_rate').css('display','none'); 549 // Hide discount checkbox 550 $('.b2bking_dynamic_rule_discount_show_everywhere_checkbox_container, .b2bking_discount_options_information_box').css('display','none'); 551 $('#b2bking_container_discountname').css('display','none'); 552 $('.b2bking_rule_label_discount').css('display','none'); 553 554 // conditions box text 555 $('#b2bking_rule_conditions_information_box_text').text(b2bkingdokan_display_settings.conditions_apply_cumulatively); 556 557 // Show all options 558 $("#b2bking_container_howmuch").css('display','inline-block'); 559 $('#b2bking_container_applies').css('display','inline-block'); 560 // Show conditions + conditions info box 561 $('#b2bking_rule_select_conditions_container').css('display','inline-block'); 562 $('.b2bking_rule_conditions_information_box').css('display','flex'); 563 564 let selectedWhat = $("#b2bking_rule_select_what").val(); 565 let selectedApplies = $("#b2bking_rule_select_applies").val(); 566 // Select Discount Amount or Percentage 567 if (selectedWhat === 'discount_amount' || selectedWhat === 'discount_percentage'){ 568 // if select Cart: cart_total_quantity and cart_total_value 569 if (selectedApplies === 'cart_total'){ 570 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value]').css('display','block'); 571 } else if (selectedApplies.startsWith("category")){ 572 // if select Category also have: category_product_quantity and category_product_value 573 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value], .b2bking_dynamic_rule_condition_name option[value=category_product_quantity], .b2bking_dynamic_rule_condition_name option[value=category_product_value]').css('display','block'); 574 } else if (selectedApplies.startsWith("product")){ 575 // if select Product also have: product_quantity and product_value 576 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value], .b2bking_dynamic_rule_condition_name option[value=product_quantity], .b2bking_dynamic_rule_condition_name option[value=product_value]').css('display','block'); 577 } else if (selectedApplies === 'multiple_options'){ 578 $('.b2bking_dynamic_rule_condition_name option').css('display','block'); 579 // conditions box text 580 $('#b2bking_rule_conditions_information_box_text').text(b2bkingdokan_display_settings.conditions_multiselect); 581 } 582 // Show discount everywhere checkbox 583 $('.b2bking_dynamic_rule_discount_show_everywhere_checkbox_container, .b2bking_discount_options_information_box').css('display','flex'); 584 $('.b2bking_rule_label_discount').css('display','block'); 585 $('#b2bking_container_discountname').css('display','inline-block'); 586 } else if (selectedWhat === 'fixed_price'){ 587 if (selectedApplies === 'cart_total'){ 588 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value]').css('display','block'); 589 } else if (selectedApplies.startsWith("category")){ 590 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=category_product_quantity]').css('display','block'); 591 } else if (selectedApplies.startsWith("product")){ 592 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=product_quantity]').css('display','block'); 593 } else if (selectedApplies === 'multiple_options'){ 594 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=category_product_quantity], .b2bking_dynamic_rule_condition_name option[value=product_quantity]').css('display','block'); 595 $('#b2bking_rule_conditions_information_box_text').text(b2bkingdokan_display_settings.conditions_multiselect); 596 } 597 } else if (selectedWhat === 'free_shipping'){ 598 // How much does not apply - hide 599 $('#b2bking_container_howmuch').css('display','none'); 600 // if select Cart: cart_total_quantity and cart_total_value 601 if (selectedApplies === 'cart_total'){
602 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value]').css('display','block'); 603 } else if (selectedApplies.startsWith("category")){ 604 // if select Category also have: category_product_quantity and category_product_value 605 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value], .b2bking_dynamic_rule_condition_name option[value=category_product_quantity], .b2bking_dynamic_rule_condition_name option[value=category_product_value]').css('display','block'); 606 } else if (selectedApplies.startsWith("product")){ 607 // if select Product also have: product_quantity and product_value 608 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value], .b2bking_dynamic_rule_condition_name option[value=product_quantity], .b2bking_dynamic_rule_condition_name option[value=product_value]').css('display','block'); 609 } else if (selectedApplies === 'multiple_options'){ 610 $('.b2bking_dynamic_rule_condition_name option').css('display','block'); 611 $('#b2bking_rule_conditions_information_box_text').text(b2bkingdokan_display_settings.conditions_multiselect); 612 } 613 } else if (selectedWhat === 'hidden_price'){ 614 // How much does not apply - hide 615 $('#b2bking_container_howmuch').css('display','none'); 616 // hide Conditions input and available conditions text 617 $('#b2bking_rule_select_conditions_container').css('display','none'); 618 $('.b2bking_rule_conditions_information_box').css('display','none'); 619 620 } else if (selectedWhat === 'required_multiple'){ 621 622 // if select Cart: cart_total_quantity and cart_total_value 623 if (selectedApplies === 'cart_total'){ 624 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value]').css('display','block'); 625 } else if (selectedApplies.startsWith("category")){ 626 // if select Category also have: category_product_quantity and category_product_value 627 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value], .b2bking_dynamic_rule_condition_name option[value=category_product_quantity], .b2bking_dynamic_rule_condition_name option[value=category_product_value]').css('display','block'); 628 } else if (selectedApplies.startsWith("product")){ 629 // if select Product also have: product_quantity and product_value 630 $('.b2bking_dynamic_rule_condition_name option[value=cart_total_quantity], .b2bking_dynamic_rule_condition_name option[value=cart_total_value], .b2bking_dynamic_rule_condition_name option[value=product_quantity], .b2bking_dynamic_rule_condition_name option[value=product_value]').css('display','block'); 631 } else if (selectedApplies === 'multiple_options'){ 632 $('.b2bking_dynamic_rule_condition_name option').css('display','block'); 633 $('#b2bking_rule_conditions_information_box_text').text(b2bkingdokan_display_settings.conditions_multiselect); 634 } 635 636 } else if (selectedWhat === 'minimum_order' || selectedWhat === 'maximum_order' ) { 637 // show Quantity/value 638 $('#b2bking_container_quantity_value').css('display','inline-block'); 639 // hide Conditions input and available conditions text 640 $('#b2bking_rule_select_conditions_container').css('display','none'); 641 $('.b2bking_rule_conditions_information_box').css('display','none'); 642 } 643 644 if (selectedApplies === 'replace_ids' && selectedWhat !== 'tax_exemption_user'){ 645 $('#b2bking_rule_select_applies_replaced_container').css('display','block'); 646 } 647 648 // Check all conditions. If selected condition what is display none,
648change to Cart Total Quantity (available for all) 649 $(".b2bking_dynamic_rule_condition_name").each(function (i) { 650 let selected = $(this).val(); 651 let selectedOption = $(this).find("option[value="+selected+"]"); 652 if (selectedOption.css('display')==='none'){ 653 $(this).val('cart_total_quantity'); 654 } 655 }); 656 657 // Update Conditions 658 updateConditionsHiddenField(); 659 } 660 661 // On condition text change, update conditions hidden field 662 $('body').on('input', '.b2bking_dynamic_rule_condition_number, .b2bking_dynamic_rule_condition_operator, .b2bking_dynamic_rule_condition_name', function () { 663 updateConditionsHiddenField(); 664 }); 665 666 function updateConditionsHiddenField(){ 667 // Clear condtions field 668 $('#b2bking_rule_select_conditions').val(''); 669 // For each condition, if not empty, add to field 670 let conditions = ''; 671 672 $(".b2bking_dynamic_rule_condition_name").each(function (i) { 673 // get its current number 674 let currentNumber; 675 let classList = $(this).attr('class').split(/\s+/); 676 $.each(classList, function(index, item) { 677 if (item.includes('identifier')) { 678 var itemArray = item.split("_"); 679 currentNumber = parseInt(itemArray[3]); 680 } 681 }); 682 683 let numberField = $(".b2bking_dynamic_rule_condition_number.b2bking_condition_identifier_"+currentNumber).val(); 684 if (numberField === undefined){ 685 numberField = ''; 686 } 687 688 if (numberField.trim() !== ''){ 689 conditions+=$(this).val()+';'; 690 conditions+=$(".b2bking_dynamic_rule_condition_operator.b2bking_condition_identifier_"+currentNumber).val()+';'; 691 conditions+=$(".b2bking_dynamic_rule_condition_number.b2bking_condition_identifier_"+currentNumber).val()+'|'; 692 } 693 }); 694 // remove last character 695 conditions = conditions.substring(0, conditions.length - 1); 696 $('#b2bking_rule_select_conditions').val(conditions); 697 } 698 699 // Save Rules 700 $('.b2bking_dokan_save_new_rule').on('click', function(){ 701 if (!$('#b2bking_new_rule_title').val()){ 702 alert(b2bkingdokan_display_settings.rule_must_have_title); 703 return; 704 } 705 706 if (confirm(b2bkingdokan_display_settings.are_you_sure_save_rule)){ 707 var checked; 708 if ($('#b2bking_dynamic_rule_discount_show_everywhere_checkbox_input').is(':checked')){ 709 checked = 1; 710 } else { 711 checked = 0; 712 } 713 714 var datavar = { 715 action: 'b2bking_save_new_ajax_rule', 716 security: b2bkingdokan_display_settings.security, 717 ruletitle: $('#b2bking_new_rule_title').val(), 718 userid: $('#b2bking_new_rule_user_id').val(), 719 newedit: $('.b2bking_dokan_save_new_rule').val(), 720 721 b2bking_rule_select_what: $('#b2bking_rule_select_what').val(), 722 b2bking_rule_select_applies: $('#b2bking_rule_select_applies').val(), 723 b2bking_rule_select_who: $('#b2bking_rule_select_who').val(), 724 b2bking_rule_select_howmuch: $('#b2bking_rule_select_howmuch').val(), 725 b2bking_rule_select_quantity_value: $('#b2bking_rule_select_quantity_value').val(), 726 b2bking_rule_select_conditions: $('#b2bking_rule_select_conditions').val(), 727 b2bking_dynamic_rule_discount_show_everywhere_checkbox_input: checked, 728 b2bking_select_multiple_users_selector_select: $('#b2bking_select_multiple_users_selector_select').val(), 729 b2bking_select_multiple_product_categories_selector_select: $('#b2bking_select_multiple_product_categories_selector_select').val(), 730 b2bking_rule_select_applies_replaced: $('#b2bking_rule_select_applies_replaced').val(), 731 732 }; 733 734 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 735 location.reload(); 736 }); 737 } 738 }); 739 740 // Delete rule 741 $('body').on('click', '.b2bking_rule_delete_table', function(){ 742 let rule = $(this).val(); 743 if (confirm(b2bkingdokan_display_settings.are_you_sure_delete_rule)){ 744 var datavar = { 745 action: 'b2bking_delete_ajax_rule', 746 security: b2bkingdokan_display_settings.security, 747 ruleid: rule, 748 userid: $('#b2bking_new_rule_user_id').val() 749 }; 750 751 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 752 location.reload(); 753 }); 754 } 755 }); 756 757 function clearRuleValues(){ 758 // clear all values 759 $('#b2bking_new_rule_title').val(''); 760 $('#b2bking_rule_select_applies').prop('selectedIndex', 0); 761 $('#b2bking_rule_select_who').prop('selectedIndex', 0); 762 $('#b2bking_rule_select_howmuch').val(''); 763 $('#b2bking_rule_select_quantity_value').prop('selectedIndex', 0); 764 $('#b2bking_select_multiple_users_selector_select').val('').trigger('change'); 765 $('#b2bking_select_multiple_product_categories_selector_select').val('').trigger('change'); 766 $('#b2bking_rule_select_applies_replaced').val(''); 767 $('.b2bking_dynamic_rule_condition_number').val(''); 768 769 // clear conditions 770 $('.b2bking_dynamic_rule_condition_delete_button').each(function(){ 771 $(this).click(); 772 }); 773 } 774 775 // on opening new rule, clear values 776 $('.b2bking_dokan_new_rule').on('click', clearRuleValues); 777 778 // Edit Rule 779 $('body').on('click', '.b2bking_rule_edit_table', function (){ 780 var rule_id = $(this).val(); 781 setTimeout(function(){ 782 // set button for save rule 783 $('.b2bking_dokan_save_new_rule').val(rule_id); 784 clearRuleValues();
785 }, 100); 786 787 // get values via AJAX and load into edit 788 // first run ajax call based on the rule id 789 790 var datavar = { 791 action: 'b2bking_get_rule_data', 792 security: b2bkingdokan_display_settings.security, 793 ruleid: rule_id, 794 userid: $('#b2bking_new_rule_user_id').val() 795 }; 796 797 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 798 setTimeout(function(){ 799 var results = response; 800 var resultsArray = results.split('*'); 801 // load values into fields 802 $('#b2bking_new_rule_title').val(resultsArray[9]); 803 $('#b2bking_rule_select_what').val(resultsArray[0]).trigger('change'); 804 $('#b2bking_rule_select_applies').val(resultsArray[1]).trigger('change'); 805 $('#b2bking_rule_select_who').val(resultsArray[2]).trigger('change'); 806 $('#b2bking_rule_select_howmuch').val(resultsArray[4]); 807 $('#b2bking_rule_select_quantity_value').val(resultsArray[3]).trigger('change'); 808 $('#b2bking_rule_select_conditions').val(resultsArray[5]); 809 810 $('.b2bking_rule_condition_container').attr('id','b2bking_condition_number_1'); 811 $('.b2bking_rule_condition_container select.b2bking_dynamic_rule_condition_name').attr('class','b2bking_dynamic_rule_condition_name b2bking_condition_identifier_1'); 812 $('.b2bking_rule_condition_container select.b2bking_dynamic_rule_condition_operator').attr('class','b2bking_dynamic_rule_condition_operator b2bking_condition_identifier_1'); 813 $('.b2bking_rule_condition_container input.b2bking_dynamic_rule_condition_number').attr('class','b2bking_dynamic_rule_condition_number b2bking_condition_identifier_1'); 814 $('.b2bking_rule_condition_container button.b2bking_dynamic_rule_condition_add_button ').attr('class','b2bking_dynamic_rule_condition_add_button b2bking_condition_identifier_1'); 815 816 var discountCheckbox = resultsArray[6]; 817 if (parseInt(discountCheckbox) === 1){ 818 $('#b2bking_dynamic_rule_discount_show_everywhere_checkbox_input').prop('checked', true); 819 } else { 820 $('#b2bking_dynamic_rule_discount_show_everywhere_checkbox_input').prop('checked', false); 821 } 822 var usersArray = resultsArray[8].split(','); 823 var productsArray = resultsArray[7].split(','); 824 $('#b2bking_select_multiple_users_selector_select').val(usersArray).trigger('change'); 825 $('#b2bking_select_multiple_product_categories_selector_select').val(productsArray).trigger('change'); 826 827 setTimeout(function(){ 828 setUpConditionsFromHidden(); 829 }, 100); 830 }, 200); 831 }); 832 }); 833 834 // on click Select all 835 $('#b2bking_select_all').on('click', function(){ 836 $('#b2bking_select_multiple_product_categories_selector_select').select2('destroy').find('#b2bking_products_optgroup option').prop('selected', 'selected').end().select2(); 837 }); 838 $('#b2bking_unselect_all').on('click', function(){ 839 jQuery("#b2bking_select_multiple_product_categories_selector_select").val(null).trigger("change"); 840 }); 841 842 /* Registration */ 843 // if user selects "I am a vendor", hide b2bking user type dropdown 844 var detached = ''; 845 $('input[type=radio][name=role]').change(function() { 846 if (this.value === 'customer') { 847 detached.insertAfter('.user-role'); 848 } else if (this.value === 'seller') { 849 detached = $('.b2bking_registration_roles_dropdown_section, .b2bking_custom_registration_container').detach(); 850 } 851 }); 852 853 /* Messages */ 854 // when page is opened, get conversation id in url and open that conversation 855 var openUrl = window.location.href; 856 var checkurl = new URL(openUrl); 857 var conversationID = checkurl.searchParams.get("conversation"); 858 if (conversationID !== null){ 859 mainTable.search( conversationID ).draw(); 860 setTimeout(function(){ 861 $('button[value='+conversationID+']').click(); 862 }, 100); 863 } 864 865 $('body').on('click', '.b2bking_conversation_table', function (){ 866 var conversation_id = $(this).val(); 867 var conversation_title = $(this).parent().parent().parent().children().eq(1).text(); 868 var conversation_type = $(this).parent().parent().parent().children().eq(2).text(); 869 var conversation_username = $(this).parent().parent().parent().children().eq(3).text(); 870 var conversation_reply = $(this).parent().parent().parent().children().eq(4).text(); 871 $('#b2bking_conversation_message_submit_vendor').val(conversation_id); 872 873 var datavar = { 874 action: 'b2bking_get_conversation_data', 875 security: b2bkingdokan_display_settings.security, 876 conversationid: conversation_id, 877 }; 878 879 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){
880 $('#b2bking_conversation_messages_container_container').empty(); 881 $(response).appendTo('#b2bking_conversation_messages_container_container'); 882 $('#b2bking_myaccount_conversation_endpoint_title').text(conversation_title); 883 $('.b2bking_myaccount_conversation_endpoint_typed').text(conversation_type); 884 $('.b2bking_myaccount_conversation_endpoint_usernamed').text(conversation_username); 885 $('.b2bking_myaccount_conversation_endpoint_replyd').text(conversation_reply); 886 887 888 }); 889 }); 890 891 $('body').on('click', '#b2bking_conversation_make_offer_vendor', function (){ 892 var conversation_id = jQuery('#b2bking_conversation_make_offer_vendor').val(); 893 window.location = b2bkingdokan_display_settings.offers_link+'?quote='+conversation_id; 894 }); 895 896 $('body').on('click', '#b2bking_conversation_message_submit_vendor', function (){ 897 var conversation_id = jQuery('#b2bking_conversation_message_submit_vendor').val(); 898 899 var datavar = { 900 action: 'b2bkingconversationmessage', 901 security: b2bkingdokan_display_settings.security, 902 message: $('#b2bking_conversation_user_new_message').val(), 903 conversationid: conversation_id, 904 }; 905 906 $.post(b2bkingdokan_display_settings.ajaxurl, datavar, function(response){ 907 // set url with vendor as parameter 908 var currentUrl = window.location.href; 909 var url = new URL(currentUrl); 910 url.searchParams.set("conversation", conversation_id); // setting your param 911 var newUrl = url.href; 912 window.location = newUrl; 913 }); 914 }); 915 916 // if vendor is set in URL, open new message 917 if (b2bkingdokan_display_settings.vendorinurl !== ''){
918 $('#b2bking_myaccount_make_inquiry_button').click(); 919 } 920 921 }); 922 923})(jQuery);
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.