SHARE
TWEET

Untitled

a guest Apr 18th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. $(document).ready(function() {
  3.  
  4.     function number_format( number, decimals, dec_point, thousands_sep ) {
  5.       // credits: https://stackoverflow.com/a/26334269/1007627
  6.       number = (number + '')
  7.         .replace(/[^0-9+\-Ee.]/g, '');
  8.       var n = !isFinite(+number) ? 0 : +number,
  9.         prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
  10.         sep  = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
  11.         dec  = (typeof dec_point === 'undefined') ? '.' : dec_point,
  12.         s    = '',
  13.         toFixedFix = function(n, prec) {
  14.           var k = Math.pow(10, prec);
  15.           return '' + (Math.round(n * k) / k)
  16.             .toFixed(prec);
  17.         };
  18.       // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  19.       s = (prec ? toFixedFix(n, prec) : '' + Math.round(n))
  20.         .split('.');
  21.       if (s[0].length > 3) {
  22.         s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  23.       }
  24.       if ((s[1] || '')
  25.         .length < prec) {
  26.         s[1] = s[1] || '';
  27.         s[1] += new Array(prec - s[1].length + 1)
  28.           .join('0');
  29.       }
  30.       return s.join(dec);
  31.     }
  32.  
  33.     function convertPrices()
  34.     {
  35.         $('.products .product').each( function() {
  36.             var shortDescription = $(this).find('.shortDescription').text();
  37.             var unit = $(this).find('.pr-list-unit').hide();
  38.  
  39.             // Shorten text
  40.             if(shortDescription.length > 200) {
  41.                 $(this).find('.shortDescription').text(shortDescription.substr(0, 200) + '...');
  42.             }
  43.  
  44.             // Calculate package size and new price per package
  45.             if(shortDescription.indexOf('Balení:') !== false){
  46.                 var price = $(this).find("meta[itemprop='price']").attr('content');
  47.                 price = price.replace(',', '.');
  48.                 price = price.replace(' ', '');
  49.                 price = price.replace('Kč','');
  50.                 price = price.trim();
  51.  
  52.                 try {
  53.                     var desc_ex = shortDescription.split('Balení:')[1].split('m')[0].replace(',', '.');
  54.                     packageSize = parseFloat(desc_ex);
  55.                 } catch(err) { return; }
  56.  
  57.                 if(isNaN(packageSize)) return;
  58.  
  59.                 console.log(packageSize);
  60.                 console.log(price);
  61.  
  62.                 $(this).find('.p-det-main-price').html(number_format(Math.round(price / packageSize) , 0,',',' ') + ' Kč / m<sup>2</sup>');
  63.                 $(this).find('.p-additional-price').html(number_format(Math.round(price / packageSize / 1.21) , 0,',',' ') + ' Kč bez DPH');
  64.                 unit.hide();
  65.             }
  66.  
  67.         });    
  68.     }
  69.  
  70.  
  71.     function getPackageSize()
  72.     {
  73.       var packageSize = $('#parameters table tr td:contains("Balení")').siblings('td').html();
  74.       if(packageSize == null) return NaN;
  75.       if(packageSize.indexOf('ks') > 0) return NaN;
  76.       packageSize = packageSize.replace(',', '.').replace('m2', '').replace('m²', '');
  77.       return parseFloat(packageSize);
  78.     }
  79.  
  80.     function showCalculator()
  81.     {
  82.       packageSize = getPackageSize();
  83.       if(isNaN(packageSize)) return;
  84.  
  85.       // Price per unit
  86.       var price = $('.price').find("meta[itemprop='price']");
  87.       if(!price.attr('content')) return;
  88.  
  89.       price = price.attr('content').replace(',', '.');
  90.       price = parseFloat(price);
  91.  
  92.       var pricePerMeter = number_format(price / packageSize, 2, ',', ' ');
  93.       var pricePerMeterNoVat = number_format(price/packageSize/1.21, 2, ',', ' ');
  94.  
  95.       // Show table for calculator
  96.       var calHtml = '';
  97.       calHtml += '<tr><td><strong>Cena/metr</strong></td><td><span class="price-highlight">'+pricePerMeter+' Kč/m<sup>2</sup> s DPH</span><br>'+pricePerMeterNoVat+' Kč/m<sup>2</sup> bez DPH</td></tr>';
  98.       calHtml += '<tr style="border-bottom: none;"><td colspan="2">';
  99.       calHtml += '<strong>Výpočet balení</strong>';
  100.       calHtml += '</tr>';
  101.  
  102.       calHtml += '<tr style="border-bottom: none;"><td class="l-col">Zadejte plochu</td><td><input type="text" class="inputSize" style=" padding: 0.25rem 1.25rem 0.25rem 0.25rem;" size="3"> m<sup>2</sup></td></tr>';
  103.       calHtml += '<tr style="display: none;border-bottom: none;"  class="packageResultShow"><td class="l-col">Potřebujete</td><td class="packageNum"></td></tr>';
  104.       calHtml += '<tr style="display: none;" class="packageResultShow"><td class="l-col">Za cenu</td><td class="totalPrice price-highlight"></td></tr>';
  105.  
  106.       $(calHtml).insertBefore($('#product-detail td.cell-price-final').parent());
  107.      
  108.       $('.td-additional-price').html(number_format(price, 2, ',', ' ') + ' Kč/balení s DPH<br>'+number_format(price/1.21, 2, ',', ' ') + ' Kč/balení bez DPH');
  109.  
  110.         // Hide main price
  111.         $('.cell-price-final .price').hide();
  112.         // Change price for price / baleni
  113.         $('.l-col strong:contains("Cena")').html('Cena/balení');
  114.  
  115.  
  116.       // Bind actions
  117.       $('.inputSize').keyup(function() {
  118.          var userInput = parseFloat($(this).val().replace(',', '.').replace(' ', ''));
  119.          if(isNaN(userInput)) { $('.packageResultShow').hide(); return; }
  120.  
  121.          var numPackages = Math.ceil(userInput / packageSize);
  122.          $('input[name="amount"]').val(numPackages);
  123.          $('.packageNum').html(numPackages + ' balení');
  124.          $('.totalPrice').html(number_format(numPackages * price , 2, ',', ' ') + ' Kč s DPH');
  125.          $('.packageResultShow').show();
  126.       });
  127.     }
  128.  
  129.     // Package calculator
  130.     showCalculator();
  131.  
  132.     // Replace baleni - za baleni
  133.     $('.cell-price-final .pr-list-unit').css('display', 'block');
  134.     $('.cell-price-final .pr-list-unit').css('font-size', '1.3rem');
  135.     //$('.cell-price-final .pr-list-unit').html($('.pr-list-unit').html().replace('/&nbsp;balení', 'za balení'));
  136.  
  137.     // Category product list prices per meter
  138.     convertPrices();
  139.     $( document ).ajaxComplete(function() {
  140.         convertPrices();
  141.     });
  142.     $('#category-tabs input').live('click', function(){
  143.         $( document ).ajaxComplete(function() {
  144.             convertPrices();
  145.         });
  146.     });
  147.     $('.pagination a').live('click', function(){
  148.         $( document ).ajaxComplete(function() {
  149.             convertPrices();
  150.         });
  151.     });
  152. </script>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top