K
K
kamisarlapsha2018-09-30 14:13:55
JavaScript
kamisarlapsha, 2018-09-30 14:13:55

How to calculate the sum of products jQuery?

Hello. I made the quantity of goods buttons, where when you click on one, the number 1 is added to the input value, and the number is subtracted from the other. But how can you make it so that when you click on the button, which, for example, increases the input value, the price is already displayed for 2 products, etc. In general, so that when you click on these buttons, the price of one more unit is added to the total amount, or it is taken away. If anything, this is all on OpenCart 2.3. On the item card. Thanks in advance. Here is the html code for the price. We believe<span class="price-tires"></span>
price-new-product product--p

<p class="product-price">
 <span class="special-prices--product">
                  <span class="price-new-product product--p">3500р.</span> <span class="price-old-product">4500р.</span>
</span>
</p>

Here is the js code for the buttons.
$(document).ready(function() {

var $input = $('.quant');
    
$('.count-down').click(function () {
    
var count = parseInt($input.val()) - 1;
count = count < 1 ? 1 : count;
$input.val(count);
$input.change();
return false;
    
});
    
$('.count-up').click(function () {
    
$input.val(parseInt($input.val()) + 1);
$input.change();
return false;
});
    
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sapfear, 2018-10-03
@kamisarlapsha

var calcSumm = function() {
  var summ = 0;
  $('.product-price').each(function() {
    var price = $('.price-new-product.product--p', this).text().replace('р.'); //лучше, если верстка будет вида
//<span class="price-new-product product--p" data-sum="3500">3500р.</span>
//тогда делаем не .text().replace('р.'), а .data('sum')
    var count = $input.val();
    summ += count*price;
  });
}

//и далее после пересчета количеств
$('.count-up').click(function () {
  ...
  calcSumm();
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question