Advertisement
customfiy

hulkcode_options.liquid

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