Tell us what you are trying to do?
I have a custom number field that is filled in from an Excel-file using WP Import. The number field only validates as a working number if there is no formatting at all. I want to have spaces as 1000-separator for easier reading but that is not allowed. When importing like this, the number field replaces the spaces with a comma and is unable to validate, thus not being useful.
Example: I want it to look like this 1 500 000 but Toolset translates it to this 1,500,000 and the only working solution I've found is this 1500000
This can be seen here:
hidden link
As these numbers are prices, It would be nice if this was possible. How can I achieve this?
Hi,
Thank you for contacting us and I'd be happy to assist.
For effective numerical comparisons, it is recommended to store numerical values without formatting data in the database, just as Toolset's number field stores.
And to show those numerical values in a specific pricing format on the front-end, you can register a custom shortcode, like this:
function format_my_number($atts)
{
$atts = shortcode_atts(
array(
'num' => '',
'sym' => '',
'sep' => '',
),
$atts
);
$num = $atts['num'];
$sep = $atts['sep'];
return $atts['sym'].' '.number_format($num, 0, '.', $sep);
}
add_shortcode('format-currency', 'format_my_number');
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
This shortcode accepts 3 attributes:
num: numeric value to format
sym: currency symbol to prepend
sep: thousand separator
In your content, you can then use this shortcode like this:
[format-currency num="[types field='number-field-slug'][/types]" sym="$" sep=" "]
In this example, I've called the value from the field with slug 'number-field-slug' to use '$' as the currency symbol and the space as the thousand separator.
regards,
Waqar
I am not sure I follow. Should I then add this shortcode in Toolset View instead of the single field that picks up the price today? When I try to do it as in the image, nothing is happening.
Yes, your understanding is correct.
If you were previously using the 'Single Field' block, you can replace it with the 'Fields and Text' block and then use the new shortcode in that.
( the build-in 'Shortcode' block doesn't handle the complex/nested shortcodes well, at times )
And in your screenshot, I noticed that the target field's slug doesn't seem correct.
( screenshot: hidden link )
Please make sure that the shortcode is using the field's slug and not the name/label.
"nybilspris" is the slug of the field. The field "Fields and text" breaks everything and the page crashed all the time when trying to work with it. It also seems to cut out most of the shortcode. The best I got was as seen in the picture and that didn't do anything. Using the shortcode doesn't do anything either.
Thank you for the screenshots.
I've updated the shortcode to include an empty value check and it is working now.
function format_my_number($atts)
{
$atts = shortcode_atts(
array(
'num' => '',
'sym' => '',
'sep' => '',
),
$atts
);
$num = $atts['num'];
$sep = $atts['sep'];
if (!empty($num)) {
return $atts['sym'].' '.number_format($num, 0, '.', $sep);
}
}
add_shortcode('format-currency', 'format_my_number');