milardovich

Some random jQuery job

Sep 5th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     function open_remodal() {
  3.         var inst = jQuery('[data-remodal-id=modal]').remodal();
  4.         /**
  5.          * Opens the modal window
  6.          */
  7.         inst.open();
  8.     }
  9.  
  10. jQuery(document).ready(
  11.     function() {
  12.         var var1;
  13.         var var2;
  14.         /*
  15.          * This is the VAT variable. Please edit this in case this changes after Brexit.
  16.          */
  17.         var vat = 0.2;
  18.         var new_design_price = 30;
  19.  
  20.         recalculateTotals = function() {
  21.             console.log("Var1: "+parseFloat(jQuery('.var1').val()));
  22.             console.log("Var2: "+parseFloat(jQuery('.var2').val()));
  23.             console.log("Var3: "+parseFloat(jQuery('.var3').val()));
  24.             console.log("Var31: "+parseFloat(jQuery('.var31').val()));
  25.             console.log("Var4: "+parseFloat(jQuery('.var4').val()));
  26.             var total = (parseFloat(jQuery('.var2').val())+parseFloat(jQuery('.var3').val())+parseFloat(jQuery('.var31').val())+(parseFloat(jQuery('.var4').val())*parseFloat(jQuery('.var1').val()))).toFixed(2);
  27.             var total_artwork_fee = (parseFloat(jQuery('.var3').val())+parseFloat(jQuery('.var31').val())).toFixed(2);
  28.             var total_customisations = (parseFloat(jQuery('.var4').val())*parseFloat(jQuery('.var1').val())).toFixed(2);
  29.             console.log("Total: "+total);
  30.             jQuery('.totals').val(total);
  31.             jQuery('.ginput_baseprice').html('£ '+(parseFloat(jQuery('.var2').val()).toFixed(2)));
  32.             jQuery('.ginput_artworkfee').html('£ '+total_artwork_fee);
  33.             jQuery('.ginput_customisationsprice').html('£ '+total_customisations);
  34.             jQuery('.ginput_total').html('£ '+total);
  35.         }
  36.  
  37.         sumjq = function(selector) {
  38.             var sum = 0;
  39.             jQuery(selector).each(function() {
  40.                 sum += Number(jQuery(this).val());
  41.             });
  42.             return sum;
  43.         }
  44.  
  45.         getbulkprice = function(var1) {
  46.             i=0;
  47.             position=0;
  48.             jQuery('.rp_wcdpd_pricing_table_quantity').each(
  49.                 function() {
  50.                     i++;
  51.                     buffer = jQuery(this).html();
  52.                     output = buffer.split("-");
  53.                     min = Number(output[0]);
  54.                     max = Number(output[1]);
  55.  
  56.                     if(i==1)
  57.                         max=min;
  58.  
  59.                     if(min <= var1 && max >= var1){
  60.                         position = i;
  61.                     }
  62.  
  63.                     if(jQuery('.rp_wcdpd_pricing_table_quantity').length == i){
  64.                             min = Number(buffer.replace("+",""));
  65.                             max = Number(buffer.replace("+",""));
  66.                     }
  67.  
  68.                     if(jQuery('.rp_wcdpd_pricing_table_quantity').length == i && var1 > max){
  69.                         position = i;
  70.                     }
  71.  
  72.                     //console.log("min: "+min);
  73.                     //console.log("max: "+max);
  74.                     //console.log("i: "+i);
  75.                    
  76.                 }
  77.             );
  78.             return Number(jQuery('.woocommerce-Price-amount').eq(position+1).html().replace('<span class="woocommerce-Price-currencySymbol">£</span>',''));
  79.         }
  80.  
  81.         jQuery('.qty_input').change(
  82.             function() {
  83.                 var1 = sumjq('.qty_input');
  84.  
  85.                 var2 = var1*getbulkprice(var1);
  86.                 jQuery('.var1').val(var1);
  87.                 jQuery('.var2').val(var2);
  88.                 //console.log('var1: '+var1);
  89.                 //console.log('var2: '+var2);
  90.                 calculateVar4();
  91.                 recalculateTotals();
  92.             }
  93.         );
  94.         jQuery('#gchoice_10_38_0, #gchoice_10_38_1, #choice_10_33_0, #choice_10_33_1, #choice_10_39_0, #choice_10_39_1').click(
  95.             function() {
  96.                 if(jQuery('#choice_10_33_0').is(':checked')){
  97.                     //jQuery('#var3').val(new_design_price);
  98.                     //console.log('var3: '+new_design_price);
  99.                     jQuery('.var3').val(parseFloat(new_design_price));
  100.                     recalculateTotals();
  101.                 } else {
  102.                     //jQuery('#var3').val(0);
  103.                     //console.log('var3: 0');
  104.                     jQuery('.var3').val(0);
  105.                     recalculateTotals();
  106.                 }
  107.             }
  108.         );
  109.  
  110.         jQuery('#gchoice_10_40_0, #gchoice_10_40_1, #choice_10_43_0, #choice_10_43_1, #choice_10_42_0, #choice_10_42_1').click(
  111.             function() {
  112.                 if(jQuery('#choice_10_43_0').is(':checked')){
  113.                     //jQuery('#var31').val(new_design_price);
  114.                     //console.log('var31: '+new_design_price);
  115.                     jQuery('.var31').val(parseFloat(new_design_price));
  116.                     recalculateTotals();
  117.                 } else {
  118.                     //jQuery('#var31').val(0);
  119.                     //console.log('var31: 0');
  120.                     jQuery('.var31').val(0);
  121.                     recalculateTotals();
  122.                 }
  123.             }
  124.         );
  125.         var step = 1;
  126.         function calculateVar4() {
  127.                
  128.                 if(step === 0){
  129.                     step = 1;
  130.                 } else {
  131.                     step = 0;
  132.                     var var4 = 0;
  133.                     var buffer = [];
  134.                     console.log(jQuery('.gfield_checkbox li.image-choices-choice-selected').length);
  135.                     if(jQuery('.gfield_checkbox li.image-choices-choice-selected').length === 0){
  136.                         jQuery('.var4').val(var4);
  137.                         recalculateTotals();
  138.                     }
  139.                     jQuery('.gfield_checkbox li.image-choices-choice-selected').each(function() {
  140.                         var classList = jQuery(this).attr('class').split(/\s+/);
  141.                         console.log(classList);
  142.                         if(buffer.indexOf(classList[0]) === -1){
  143.                             buffer.push(classList[0]);
  144.                             console.log(classList);
  145.                             //var classList = jQuery(this).attr('class').split(/\s+/);
  146.                             jQuery.each(classList, function(index, item) {
  147.                                 if (Number(item.indexOf("gchoice")) === 0 && index === 0) {
  148.                                     //console.log(product_title = jQuery('#choice_'+item.replace("gchoice_","")).val());
  149.                                    var product_title = jQuery('#choice_'+item.replace("gchoice_","")).val();
  150.                                    if(product_title !== null){
  151.                                         var request = jQuery.ajax({
  152.                                           url: "/wp-json/wooaddons/v1/discountprice/"+product_title+"/"+jQuery('.var1').val(),
  153.                                           method: "GET",
  154.                                           dataType: "json"
  155.                                         });
  156.                                          
  157.                                         request.done(function( msg ) {
  158.                                             console.log(msg);
  159.                                           if(!isNaN(msg)){
  160.                                             var4 = var4+(vat*parseFloat(msg))+parseFloat(msg);
  161.                                             //console.log("var4: "+var4);
  162.                                             jQuery('.var4').val(var4);
  163.                                             recalculateTotals();
  164.                                           }
  165.                                         });
  166.                                          
  167.                                         request.fail(function( jqXHR, textStatus ) {
  168.                                           console.log( "Request failed: " + textStatus );
  169.                                         });
  170.                                    }
  171.  
  172.                                 }
  173.                             });
  174.                         }
  175.                     });
  176.                     return var4;
  177.                 }
  178.             }
  179.  
  180.             jQuery('ul.gfield_checkbox li.image-choices-choice').click( function() { calculateVar4(); } );
  181.  
  182.     }
  183. );
Advertisement
Add Comment
Please, Sign In to add comment