Advertisement
customfiy

checked option hulk custoify

May 17th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.24 KB | None | 0 0
  1. <div id="hulkapps_custom_options_{{ product.id }}"></div>
  2. <script>
  3. var pid = {{ product.id }};
  4. if(typeof(jQuery) == "undefined"){
  5. window.jQuery || document.write('<script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"><\/script>')
  6. }else if(jQuery.fn.jquery == "3.2.1"){
  7. $.getScript("https://productoption.hulkapps.com/bootstrap-tooltip.js", function(){
  8. var store_id = "{{shop.permanent_domain}}";
  9. $.ajax({
  10. type:"GET",
  11. url: "https://productoption.hulkapps.com/store/get_all_relationships",
  12. data : { pid : pid,store_id : store_id},
  13. crossDomain: true,
  14. success:function(data){
  15. $("#hulkapps_custom_options_{{ product.id }}").html(data);
  16. setTimeout(function(){
  17. $('[data-toggle="tooltip"]').tooltip();
  18. conditional_rules(pid);
  19. $('#hulkapps_options_' + pid).closest("form").find(':submit').addClass('hulkapps_submit_cart myclasss');
  20. $(".hulkapps_submit_cart.myclasss").prop('disabled', true);
  21. }, 500);
  22.  
  23. }
  24. });
  25. });
  26. }else{
  27. $.getScript("//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js", function(){
  28. $.noConflict( true );
  29. $.getScript("https://productoption.hulkapps.com/bootstrap-tooltip.js", function(){
  30. var store_id = "{{shop.permanent_domain}}";
  31. $.ajax({
  32. type:"GET",
  33. url: "https://productoption.hulkapps.com/store/get_all_relationships",
  34. data : { pid : pid,store_id : store_id},
  35. crossDomain: true,
  36. success:function(data){
  37. $("#hulkapps_custom_options_{{ product.id }}").html(data);
  38. setTimeout(function(){
  39. $('[data-toggle="tooltip"]').tooltip();
  40. conditional_rules(pid);
  41. $('#hulkapps_options_' + pid).closest("form").find(':submit').addClass('hulkapps_submit_cart myclasss');
  42. $(".hulkapps_submit_cart.myclasss").prop('disabled', true);
  43. $(".hulkapps_check_option .price-change").click(function() {
  44. if (this.checked) {
  45. $(".hulkapps_submit_cart.myclasss").prop('disabled', false);
  46. }else{
  47. $(".hulkapps_submit_cart.myclasss").prop('disabled', true);
  48. }
  49. });
  50.  
  51. }, 500);
  52. }
  53. });
  54. });
  55. });
  56. }
  57. function conditional_rules(prod_id) {
  58. pass=false;
  59. verify_all = Array();
  60. verify_any = Array();
  61. verified_condition = Array();
  62. pass_array = Array();
  63. $("#hulkapps_option_list_" + prod_id + " .condition_hide").removeClass("conditional");
  64. $("#hulkapps_option_list_" + prod_id + " .condition_show").addClass("conditional");
  65. $("#hulkapps_custom_options_" + prod_id + " #conditional_rules").children().each(function() {
  66. pass_array = Array();
  67. pass=false;
  68. $(this).children().each(function() {
  69. pass=false;
  70. var condition_rule = $(this).text();
  71. var field_value;
  72. if ($("#hulkapps_option_list_" + prod_id + " .option_type_id_" + $(this).attr("data-field-num")).hasClass("dd_multi_render") == true) {
  73. var aa = condition_rule;
  74. if (aa.indexOf("!=") >= 0){pass=true;}
  75. var count = $("#hulkapps_option_list_" + prod_id + " .hulkapps_option_" + $(this).attr("data-field-num") + "_visible:visible :selected").length;
  76. var ct=1;
  77. var selected_array = Array();
  78. if($("#hulkapps_option_list_" + prod_id + " .hulkapps_option_" + $(this).attr("data-field-num") + "_visible:visible :selected").length > 0){
  79. $("#hulkapps_option_list_" + prod_id + " .hulkapps_option_" + $(this).attr("data-field-num") + "_visible:visible :selected").each(function(){
  80. var condition_rule=aa;
  81. field_value = $(this).data("conditional-value");
  82. condition_rule = condition_rule.replace("**value11**", field_value);
  83. if (condition_rule.indexOf("==") >= 0){
  84. var condition_rule = condition_rule.split("==");
  85. if (condition_rule[0] == condition_rule[1]) {
  86. pass = true;
  87. } else {
  88. pass = false;
  89. }
  90. }
  91. else{
  92. var condition_rule = condition_rule.split("!=");
  93. if (condition_rule[0] != condition_rule[1]) {
  94. pass = true;
  95. } else {
  96. pass = false;
  97. }
  98. }
  99. selected_array.push(pass);
  100. if(ct==count && count>1){
  101.  
  102. var result = selected_array.join(' || ');
  103. result = eval(result);
  104. pass_array.push(result);
  105. }
  106. else if(count==1){
  107. pass_array.push(pass);
  108. }
  109. ct=ct+1;
  110. });
  111. }else{
  112. pass_array.push(false);
  113. }
  114. }
  115. else if ($("#hulkapps_option_list_" + prod_id + " .option_type_id_" + $(this).attr("data-field-num")).hasClass("cb_render") == true) {
  116. var aa = condition_rule;
  117. if (aa.indexOf("!=") >= 0){pass=true;}
  118. var ctt=1;
  119. var checked_array = Array();
  120. var countt = $("#hulkapps_option_list_" + prod_id + " .hulkapps_option_" + $(this).attr("data-field-num") + "_visible:visible:checked").length;
  121. $("#hulkapps_option_list_" + prod_id + " .hulkapps_option_" + $(this).attr("data-field-num") + "_visible:visible:checked").each(function() {
  122. var condition_rule=aa;
  123. field_value = $(this).data("conditional-value");
  124. condition_rule = condition_rule.replace("**value11**", field_value);
  125. if (condition_rule.indexOf("==") >= 0){
  126. var condition_rule = condition_rule.split("==");
  127. if (condition_rule[0] == condition_rule[1]) {
  128. pass = true;
  129. } else {
  130. pass = false;
  131. }
  132. }
  133. else{
  134. var condition_rule = condition_rule.split("!=");
  135. if (condition_rule[0] != condition_rule[1]) {
  136. pass = true;
  137. } else {
  138. pass = false;
  139. }
  140. }
  141. checked_array.push(pass);
  142. if(ctt==countt && countt>1){
  143. var result = checked_array.join(' || ');
  144. result = eval(result);
  145. pass_array.push(result);
  146. }
  147. else if(countt==1){
  148. pass_array.push(pass);
  149. }
  150. ctt=ctt+1;
  151. });
  152. }
  153. else {
  154. pass=false;
  155. if ($("#hulkapps_option_list_" + prod_id + " .option_type_id_" + $(this).attr("data-field-num")).hasClass("rb_render") == true) {
  156. field_value = $("#hulkapps_option_list_" + prod_id + " .hulkapps_option_" + $(this).attr("data-field-num") + ":checked").data("conditional-value");
  157. }
  158. else if ($("#hulkapps_option_list_" + prod_id + " .option_type_id_" + $(this).attr("data-field-num")).hasClass("dd_render") == true) {
  159. field_value = $("#hulkapps_option_list_" + prod_id + " #" + $(this).attr("data-field-num") + " option:selected").data("conditional-value");
  160. }
  161. else if ($("#hulkapps_option_list_" + prod_id + " .option_type_id_" + $(this).attr("data-field-num")).hasClass("swatch_render") == true) {
  162. field_value = $("#hulkapps_option_list_" + prod_id + " .hulkapps_option_" + $(this).attr("data-field-num")+ ".swatch_selected").data("conditional-value");
  163. }
  164. else {
  165. field_value = $("#hulkapps_option_list_" + prod_id + " #" + $(this).attr("data-field-num") + "").val();
  166. }
  167. condition_rule = condition_rule.replace("**value11**", field_value);
  168. if (condition_rule.indexOf("==") >= 0){
  169. var condition_rule = condition_rule.split("==");
  170. if (condition_rule[0] == condition_rule[1]) {
  171. pass = true;
  172. } else {
  173. pass = false;
  174. }
  175. }
  176. else{
  177. var condition_rule = condition_rule.split("!=");
  178. if (condition_rule[0] != condition_rule[1]) {
  179. pass = true;
  180. } else {
  181. pass = false;
  182. }
  183. }
  184. pass_array.push(pass);
  185. }
  186. });
  187.  
  188. var type_rule = $(this).attr("data-verify-all");
  189. var condition_id = $(this).attr("name");
  190. if(type_rule=="0"){
  191. var res = pass_array.join(' || ');
  192. }
  193. else{
  194. var res = pass_array.join(' && ');
  195. }
  196. res = eval(res);
  197. if(res){
  198. $("#hulkapps_option_list_" + prod_id + " ." + condition_id + "_show").removeClass("conditional");
  199. $("#hulkapps_option_list_" + prod_id + " ." + condition_id + "_hide").addClass("conditional");
  200. $("#hulkapps_option_list_" + prod_id + " ." + condition_id + "_hide.conditional").find('.hulkapps_option_child').each(function() {
  201. conditional_change($(this));
  202. });
  203. }
  204. else{
  205. $("#hulkapps_option_list_" + prod_id + " ." + condition_id + "_show.conditional").find('.hulkapps_option_child').each(function() {
  206. conditional_change($(this));
  207. });
  208. }
  209. });
  210. calc_options_total(prod_id);
  211. border_bottom = $(".hulkapps_option:visible").css("border-bottom");
  212. $(".hulkapps_option:visible").css("border-bottom",border_bottom);
  213. $(".hulkapps_option:visible:last").css("border-bottom","none");
  214. }
  215. function conditional_change(obj){
  216. if (obj.prop("type") == "select-one" || obj.prop("type") == "select-multiple") {
  217. if (obj.val() != ''){
  218. obj.val('').change();
  219. }
  220. }
  221. else if (obj.prop("type") == "radio") {
  222. if (obj.prop("checked")){
  223. obj.prop("checked", false);
  224. obj.val('');
  225. obj.parent().find('.radio_selected').removeClass("radio_selected");
  226. }
  227. }
  228. else if (obj.prop("type") == "textarea" || obj.prop("type") == "text" || obj.prop("type") == "hidden" || obj.prop("type") == "file"){
  229. if (obj.val()){
  230. obj.val('').change();
  231. }
  232. }
  233. else if (obj.prop("type") == "checkbox") {
  234. if (obj.prop("checked")){
  235. obj.prop("checked", false);
  236. }
  237. }
  238. else if (obj.prop("tagName") == "DIV") {
  239. if (obj.find('.swatch_radio').prop("checked")){
  240. obj.find('.swatch_radio').prop("checked", false);
  241. obj.removeClass("swatch_selected");
  242. }
  243. }
  244. }
  245.  
  246. function calc_options_total(product_id) {
  247. var i;
  248. var total = 0;
  249. var format ={{ shop.money_format | json }};
  250.  
  251. checked_variant = $("#hulkapps_option_list_" + product_id + ":visible .price-change:checked, #hulkapps_option_list_" + product_id + ":visible .price-change:selected, .hulkapps_swatch_option .swatch_selected,.textarea_selected,.textbox_selected");
  252. for (i = 0; i < checked_variant.length; i++) {
  253. if(!$(checked_variant[i]).parents(".hulkapps_option").hasClass('conditional')){
  254. total = Number($(checked_variant[i]).attr("data-price")) + Number(total);
  255. }
  256. }
  257. $("#hulkapps_options_" + product_id + " #raw_option_total").val(total);
  258. $("#hulkapps_options_" + product_id + " #calculated_option_total").html(total.toFixed(2));
  259. if (total > 0 && 1) {
  260. $('#hulkapps_options_' + product_id + ' #option_total').slideDown();
  261. } else {
  262. $('#hulkapps_options_' + product_id + ' #option_total').slideUp();
  263. }
  264. }
  265.  
  266. function validate_options(product_id) {
  267. var good = true;
  268. $(".hulkapps_option:visible").each(function() {
  269. $(this).removeClass("validation_error");
  270. });
  271. $('#hulkapps_options_' + product_id + ' #error_text').html('');
  272. var hulkapps_req = $("#hulkapps_option_list_" + product_id + ":visible .required:visible");
  273. var i;
  274. for (i = 0; i < hulkapps_req.length; i++) {
  275. if ($(hulkapps_req[i]).find("select[name^='properties']").length == 1 && !$(hulkapps_req[i]).find("select[name^='properties']").val()) {
  276. $(hulkapps_req[i]).addClass("validation_error");
  277. good = false;
  278. } else if ($(hulkapps_req[i]).find(".hulkapps_radio_option").length && !$(hulkapps_req[i]).find("input[name^='properties']:checked").length) {
  279. $(hulkapps_req[i]).addClass("validation_error");
  280. good = false;option_display_total_format
  281. }
  282. else if ($(hulkapps_req[i]).find(".hulkapps_swatch_option").length && !$(hulkapps_req[i]).find("input[name^='properties']:checked").length) {
  283. $(hulkapps_req[i]).addClass("validation_error");
  284. good = false;
  285. } else if ($(hulkapps_req[i]).find("input[type='text']").length > 1) {
  286. $(hulkapps_req[i]).find("input[type='text']").each(function(){
  287. if($(this).val() == ''){
  288. $(hulkapps_req[i]).addClass("validation_error");
  289. good = false;
  290. }
  291. });
  292. } else if ($(hulkapps_req[i]).find("input[type='text']").length && !$(hulkapps_req[i]).find("input[name^='properties']").val()) {
  293. $(hulkapps_req[i]).addClass("validation_error");
  294. good = false;
  295. } else if ($(hulkapps_req[i]).find(".hulkapps_check_option").length && !$(hulkapps_req[i]).find("input[name^='properties']").val()) {
  296. $(hulkapps_req[i]).addClass("validation_error");
  297. good = false;
  298. } else if ($(hulkapps_req[i]).find("input[type='file']").length && !$(hulkapps_req[i]).find("input[name^='properties']").val()) {
  299. $(hulkapps_req[i]).addClass("validation_error");
  300. good = false;
  301. } else if ($(hulkapps_req[i]).hasClass("cb_render") && $(hulkapps_req[i]).find("input[type='checkbox']:checked").length && !$(hulkapps_req[i]).find("input[name^='properties']").length) {
  302. $(hulkapps_req[i]).addClass("validation_error");
  303. good = false;
  304. } else if ($(hulkapps_req[i]).find("textarea").length && !$(hulkapps_req[i]).find("input[name^='properties']").val()) {
  305. $(hulkapps_req[i]).addClass("validation_error");
  306. good = false;
  307. } else if ($(hulkapps_req[i]).find("select[multiple]").length && !$(hulkapps_req[i]).find("input[name^='properties']").val()) {
  308. $(hulkapps_req[i]).addClass("validation_error");
  309. good = false;
  310. } else {
  311. $(hulkapps_req[i]).removeClass("validation_error");
  312. }
  313. }
  314. error_output = "";
  315.  
  316. if (good && typeof additional_product_option_validation != "undefined") {
  317. good = additional_product_option_validation();
  318. }
  319. if (typeof notify_validation != "undefined") {
  320. notify_validation(good);
  321. }
  322. if (!good) {
  323. $('#hulkapps_options_' + product_id + ' #error_text').html(error_output);
  324. $('#hulkapps_options_' + product_id + ' #error_text').slideDown();
  325. }
  326. return good;
  327. }
  328. function validate_single_option(option_type_id,option_type) {
  329.  
  330. if(option_type == 'dd_render'){
  331. if ($('.'+option_type_id).find("select[name^='properties']").length == 1 && !$('.'+option_type_id).find("select[name^='properties']").val() && $('.'+option_type_id).hasClass('required')) {
  332. $('.'+option_type_id).addClass("validation_error");
  333. }else{
  334. $('.'+option_type_id).removeClass("validation_error");
  335. }
  336. }
  337.  
  338. else if(option_type == 'dd_multi_render'){
  339. if ($('.'+option_type_id).find("select[multiple]").length && !$('.'+option_type_id).find("input[name^='properties']").val() && $('.'+option_type_id).hasClass('required')) {
  340. $('.'+option_type_id).addClass("validation_error");
  341. } else {
  342. $('.'+option_type_id).removeClass("validation_error");
  343. }
  344. }
  345.  
  346. else if(option_type == 'swatch_render'){
  347. if ($('.'+option_type_id).find(".hulkapps_swatch_option").length && !$('.'+option_type_id).find("input[name^='properties']:checked").length && $('.'+option_type_id).hasClass('required')) {
  348. $('.'+option_type_id).addClass("validation_error");
  349. }else {
  350. $('.'+option_type_id).removeClass("validation_error");
  351. }
  352.  
  353. }
  354.  
  355. else if(option_type == 'cb_render'){
  356. if ($('.'+option_type_id).find(".hulkapps_check_option").length && !$('.'+option_type_id).find("input[name^='properties']").val() && $('.'+option_type_id).hasClass('required')) {
  357. $('.'+option_type_id).addClass("validation_error");
  358. }else {
  359. $('.'+option_type_id).removeClass("validation_error");
  360. }
  361. }
  362.  
  363. else if(option_type == 'tb_render'){
  364. if ($('.'+option_type_id).find("input[type='text']").length && !$('.'+option_type_id).find("input[name^='properties']").val() && $('.'+option_type_id).hasClass('required')) {
  365. $('.'+option_type_id).addClass("validation_error");
  366. }else {
  367. $('.'+option_type_id).removeClass("validation_error");
  368. }
  369. }
  370.  
  371. else if(option_type == 'ta_render'){
  372. if ($('.'+option_type_id).find("textarea").length && !$('.'+option_type_id).find("input[name^='properties']").val() && $('.'+option_type_id).hasClass('required')) {
  373. $('.'+option_type_id).addClass("validation_error");
  374. }else {
  375. $('.'+option_type_id).removeClass("validation_error");
  376. }
  377. }
  378.  
  379. else if(option_type == 'rb_render'){
  380. if ($('.'+option_type_id).find(".hulkapps_radio_option").length && !$('.'+option_type_id).find("input[name^='properties']:checked").length && $('.'+option_type_id).hasClass('required')) {
  381. $('.'+option_type_id).addClass("validation_error");
  382. }else {
  383. $('.'+option_type_id).removeClass("validation_error");
  384. }
  385. }
  386.  
  387. else if(option_type == 'fu_render'){
  388. if ($('.'+option_type_id).find("input[type='file']").length && !$('.'+option_type_id).find("input[name^='properties']").val() && $('.'+option_type_id).hasClass('required')) {
  389. $('.'+option_type_id).addClass("validation_error");
  390. }else {
  391. $('.'+option_type_id).removeClass("validation_error");
  392. }
  393. }
  394.  
  395. }
  396. var hulk_flag = 0;
  397. $("body").on('click', '.hulkapps_submit_cart', function(e) {
  398. if(hulk_flag == 0){
  399. e.preventDefault();
  400. var res = true;
  401. if (validate_options(pid)) {
  402. hulk_flag = 1;
  403. $('.hulkapps_submit_cart').click();
  404. }
  405. };
  406. });
  407.  
  408. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement