D
D
Dr Zhmurge2019-12-26 18:36:30
JavaScript
Dr Zhmurge, 2019-12-26 18:36:30

How to change the price of a Woocommerce item using jQuery?

I need to make 2 prices in the product card: per ton and per linear meter. Like here: A
5e04d176898b9810599863.png
live example at the link: https://vestametall.ru/sortovoj-i-fasonnyj-prokat/...
In order to implement all this, I used a simple code:

$('body').on('change', '.qty', function() {
    var w = $(this).data('w').toString().replace(/,/g, '.').replace(/ /g, ''),   /*вес погонного метра*/
    price = $(this).data('price').toString().replace(/,/g, '.').replace(/ /g, ''),    /*стоимость тонны*/
    count = $(this).val().toString().replace(/,/g, '.').replace(/ /g, ''),   /*количество тонн*/
    wrapper = $(this).closest('.quantity__input__wrapper');
console.log('price='+price+'count='+count+'wrapper='+wrapper);
    metr = parseInt(count/w);
    totalPrice = parseFloat(count*price);  /*получаем цену*/
console.log('metr='+metr+'totalPrice='+totalPrice);
    $('.woodmart-compare-btn').text(metr);
    $('.woocommerce-Price-amount').text(totalPrice); /*записываем цену куда то*/
  });

  $('body').on('change', '.quantity__input__metr', function() {
    var wrapper =  $(this).closest('.quantity__input__wrapper'),
    inp = $('.quantity__input__main', wrapper),
    w = inp.data('w').toString().replace(/,/g, '.').replace(/ /g, ''),
    metr = $(this).val().toString().replace(/,/g, '.').replace(/ /g, '');
    count = round(metr*w);
    inp.val(count).trigger('change');
  });

I get the price I want. But how do you get Woocommerce to take that price as the price of the product when adding to cart and for later checkout?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question