Skip Navigation

[Gelöst] Types Number Field Set Number Of Decimal Places To 2 (For Product Price in GBP)

This support ticket is created vor 7 Jahre, 6 Monate. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 8 Antworten, has 2 Stimmen.

Last updated by markW-13 vor 7 Jahre, 5 Monate.

Assisted by: Luo Yang.

Author
Artikel
#448709

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).

I found this:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_custom_field_filter_type/
...but do not understand it.

Please let me know if I am missing something obvious?

(If the functionality is not possible in Types Numeric fields, I can pay my external resource programmer to apply some front-end Javascript - something like this http://stackoverflow.com/questions/22641074/html5-number-input-always-show-2-decimal-places).

#448989

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

#449075
number-field-2.JPG
number-field.JPG

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

#449612

According to our document:
https://toolset.com/documentation/customizing-sites-using-php/functions/#numeric
There isn't such an option to "Set Number Of Decimal Places To 2".

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, '.', '');
}

More help:
hidden link

#449705
wp-back-office-price-now-OK.JPG
product-content-template-prices.JPG
functions-decimal-changes.JPG
front-end-price-still-INCORRECT.JPG

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.

#449992

You will need to clear the cache by updating the problem post, for example, edit the problem post, click button "Update", and test again

#450082

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.

#450415

Please modify the PHP codes as below:

$field_slugs = array(
	'num-5',
	'num-6',
	//here add more field slugs
);
foreach($field_slugs as $field_slug){
	add_filter( 'wpcf_fields_slug_' . $field_slug . '_value_get', 'Decimal_Places_func');
	add_filter( 'wpcf_fields_slug_' . $field_slug . '_value_display', 'Decimal_Places_func');
}
function Decimal_Places_func($value){
	return number_format($value, 2, '.', ',');
}
#450518

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).

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.