[Resolved] Types Number Field Set Number Of Decimal Places To 2 (For Product Price in GBP)
This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
I can't find anything newer than 2 years old on this topic, but it was suggested on March 24, 2014 by 'chi-lokL' at https://toolset.com/forums/topic/views-filter-on-numeric-field-not-working/ that 'our next version is going to implement a filter so you can adjust that values with a little piece of code'.
I need to be able to Round my Types fields for unit prices to 2 decimal places (always, never zero or 1 or more than 2).
Languages: English (English )Chinese (Simplified) (简体中文 )
Timezone: Asia/Hong_Kong (GMT+08:00)
Dear Mark,
Could you describe more details for the question:
I need to be able to Round my Types fields for unit prices to 2 decimal places (always, never zero or 1 or more than 2).
I need detail steps to test it in my localhost, thanks
Thank you for your reply. All of the Price fields are set up in exactly the same way, hopefully the screenshots attached will show you the problem.
This could be a problem that WordPress has with numeric fields, but I was hoping that there was a way to customise the Custom Post Field using the Types software from Toolset so that any number always has 2 decimal places, even if those decimals are zeros. This will ensure consistent presentation on the front end view for our website visitors.
1.7 should be 1.70
0.6 should be 0.60
4 should be 4.00
10 should be 10.00
55.1 should be 55.10
But you can use Types filter hook "wpcf_fields_slug_' . $field_slug . '_value_get" to do it, for example, add below codes into your theme/functions.php:
$field_slugs = array(
'num-1',
'num-2',
//here add more field slugs
);
foreach($field_slugs as $field_slug){
add_filter( 'wpcf_fields_slug_' . $field_slug . '_value_get', 'Decimal_Places_func');
}
function Decimal_Places_func($value){
return number_format($value, 2, '.', '');
}
Hello Luo,
Thank you - I have nearly solved this issue now! However, despite the successful changes you kindly provided for my functions.php file (see attached) resulting in the 'back office' values displaying correctly with 2 decimal places, the 'front end' display of my product is still rounding up / ignoring the extra zero in cases such as the one demonstrated by the screenshots attached.
In the template (see screenshots) I have the output stated as 'raw' so I cannot work out why this still isn't giving me the display change I need.
Thank you for this response - so I need to find the affected posts individually from several thousand individual 'Products' and go to the individual Edit page for each and click on the Update button?
It'd be great if there was a recommended program/plugin or Script to do this within Types.
Hello Luo,
Thank you for this addition, which appears to be working exactly as intended. Hopefully this will also help others with a similar issue.
(For any future viewers of this post who then experience a problem with a price showing as '0.00' after an individual post (or product) has been updated, this can be excluded using Toolset's excellent Conditional Output for display/non-display).