Woocommerce add thousand separator in custom price field

Viewed 46

I want to add a thousand separator in custom input field in Woocommerce product page.

this is my code:

jQuery(document).ready(function () {
  jQuery(document).on('input', '.number-separator', function (e) {
    if (/^[0-9.,]+$/.test(jQuery(this).val())) {
      jQuery(this).val(
        parseFloat(jQuery(this).val().replace(/,/g, '')).toLocaleString('en')
      );
    } else {
      jQuery(this).val(
        jQuery(this)
          .val()
          .substring(0, jQuery(this).val().length - 1)
      );
    }
  });
}); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="tmcp_textfield_6" class="number-separator" value="" >

Everything seems right, But when I add the product to the cart Only the first part of the price is added.

For example, when I enter the number 5,000,000 Only number 5 will be added to the cart

1 Answers

from funcion file

// Woocommerce add thousand separator in custom price field
add_filter('woocommerce_price_format', 'digitalize_price_format');
function digitalize_price_format($format) {
    $format['decimal_point'] = '.';
    $format['thousand_sep'] = ',';
    return $format;
}
Related