1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Product Page
  5. Enable decimals in Number fields by filtering the step value
  1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Enable decimals in Number fields by filtering the step value
  1. Home
  2. Knowledge Base
  3. Advanced Topics for Add-Ons Ultimate
  4. Enable decimals in Number fields by filtering the step value

Enable decimals in Number fields by filtering the step value

By default, Number fields can be increased in increments of 1. If you want to change this value, e.g. to use decimals, add this snippet:

<?php
/**
* Filter the step parameter in Number fields to return decimals
*/
function prefix_number_field_step( $step, $item ) {
return 0.01;
}
add_filter( 'pewc_number_field_step', 'prefix_number_field_step', 10, 2 );
/**
* Filter the step parameter in some Number fields to return decimals
*/
function prefix_specific_number_field_step( $step, $item ) {
if( $item['field_id'] == 1234 ) { // Change this to the ID of your field
return 0.01;
}
return $step;
}
add_filter( 'pewc_number_field_step', 'prefix_specific_number_field_step', 10, 2 );

Here’s how to add a snippet to your site.

Filtering the minimum / maximum step values

You can use a similar filter on the Min Value and Max Value settings for Number fields.

<?php
/**
* Filter the step value for min and max values
*/
function prefix_min_max_val_step( $step, $item ) {
return 0.01;
}
add_filter( 'pewc_min_max_val_step', 'prefix_min_max_val_step', 10, 2 );

Was this article helpful?

Related Articles