Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- $(document).ready(function() {
- function number_format( number, decimals, dec_point, thousands_sep ) {
- // credits: https://stackoverflow.com/a/26334269/1007627
- number = (number + '')
- .replace(/[^0-9+\-Ee.]/g, '');
- var n = !isFinite(+number) ? 0 : +number,
- prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
- sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
- dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
- s = '',
- toFixedFix = function(n, prec) {
- var k = Math.pow(10, prec);
- return '' + (Math.round(n * k) / k)
- .toFixed(prec);
- };
- // Fix for IE parseFloat(0.55).toFixed(0) = 0;
- s = (prec ? toFixedFix(n, prec) : '' + Math.round(n))
- .split('.');
- if (s[0].length > 3) {
- s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
- }
- if ((s[1] || '')
- .length < prec) {
- s[1] = s[1] || '';
- s[1] += new Array(prec - s[1].length + 1)
- .join('0');
- }
- return s.join(dec);
- }
- function convertPrices()
- {
- $('.products .product').each( function() {
- var shortDescription = $(this).find('.shortDescription').text();
- var unit = $(this).find('.pr-list-unit').hide();
- // Shorten text
- if(shortDescription.length > 200) {
- $(this).find('.shortDescription').text(shortDescription.substr(0, 200) + '...');
- }
- // Calculate package size and new price per package
- if(shortDescription.indexOf('Balení:') !== false){
- var price = $(this).find("meta[itemprop='price']").attr('content');
- price = price.replace(',', '.');
- price = price.replace(' ', '');
- price = price.replace('Kč','');
- price = price.trim();
- try {
- var desc_ex = shortDescription.split('Balení:')[1].split('m')[0].replace(',', '.');
- packageSize = parseFloat(desc_ex);
- } catch(err) { return; }
- if(isNaN(packageSize)) return;
- console.log(packageSize);
- console.log(price);
- $(this).find('.p-det-main-price').html(number_format(Math.round(price / packageSize) , 0,',',' ') + ' Kč / m<sup>2</sup>');
- $(this).find('.p-additional-price').html(number_format(Math.round(price / packageSize / 1.21) , 0,',',' ') + ' Kč bez DPH');
- unit.hide();
- }
- });
- }
- function getPackageSize()
- {
- var packageSize = $('#parameters table tr td:contains("Balení")').siblings('td').html();
- if(packageSize == null) return NaN;
- if(packageSize.indexOf('ks') > 0) return NaN;
- packageSize = packageSize.replace(',', '.').replace('m2', '').replace('m²', '');
- return parseFloat(packageSize);
- }
- function showCalculator()
- {
- packageSize = getPackageSize();
- if(isNaN(packageSize)) return;
- // Price per unit
- var price = $('.price').find("meta[itemprop='price']");
- if(!price.attr('content')) return;
- price = price.attr('content').replace(',', '.');
- price = parseFloat(price);
- var pricePerMeter = number_format(price / packageSize, 2, ',', ' ');
- var pricePerMeterNoVat = number_format(price/packageSize/1.21, 2, ',', ' ');
- // Show table for calculator
- var calHtml = '';
- 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>';
- calHtml += '<tr style="border-bottom: none;"><td colspan="2">';
- calHtml += '<strong>Výpočet balení</strong>';
- calHtml += '</tr>';
- 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>';
- calHtml += '<tr style="display: none;border-bottom: none;" class="packageResultShow"><td class="l-col">Potřebujete</td><td class="packageNum"></td></tr>';
- calHtml += '<tr style="display: none;" class="packageResultShow"><td class="l-col">Za cenu</td><td class="totalPrice price-highlight"></td></tr>';
- $(calHtml).insertBefore($('#product-detail td.cell-price-final').parent());
- $('.td-additional-price').html(number_format(price, 2, ',', ' ') + ' Kč/balení s DPH<br>'+number_format(price/1.21, 2, ',', ' ') + ' Kč/balení bez DPH');
- // Hide main price
- $('.cell-price-final .price').hide();
- // Change price for price / baleni
- $('.l-col strong:contains("Cena")').html('Cena/balení');
- // Bind actions
- $('.inputSize').keyup(function() {
- var userInput = parseFloat($(this).val().replace(',', '.').replace(' ', ''));
- if(isNaN(userInput)) { $('.packageResultShow').hide(); return; }
- var numPackages = Math.ceil(userInput / packageSize);
- $('input[name="amount"]').val(numPackages);
- $('.packageNum').html(numPackages + ' balení');
- $('.totalPrice').html(number_format(numPackages * price , 2, ',', ' ') + ' Kč s DPH');
- $('.packageResultShow').show();
- });
- }
- // Package calculator
- showCalculator();
- // Replace baleni - za baleni
- $('.cell-price-final .pr-list-unit').css('display', 'block');
- $('.cell-price-final .pr-list-unit').css('font-size', '1.3rem');
- //$('.cell-price-final .pr-list-unit').html($('.pr-list-unit').html().replace('/ balení', 'za balení'));
- // Category product list prices per meter
- convertPrices();
- $( document ).ajaxComplete(function() {
- convertPrices();
- });
- $('#category-tabs input').live('click', function(){
- $( document ).ajaxComplete(function() {
- convertPrices();
- });
- });
- $('.pagination a').live('click', function(){
- $( document ).ajaxComplete(function() {
- convertPrices();
- });
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement