Skip Navigation

[Resolved] display numeric value as money format

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to format their number field as currency.

Solution:

This can be done by using the shortcode below.

function format_my_number($atts)
{
    $atts = shortcode_atts(
        array(
        'num' => '',
        'sym' => '',
    ),
        $atts
    );
    $num = $atts['num'];
  
    return $atts['sym'].' '.number_format($num, 0, '.', ',');
}
add_shortcode('format-currency', 'format_my_number');

So it would be [format-currency num="types shortcode goes here" sym="$"]

This support ticket is created 5 years, 11 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.

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 8 replies, has 2 voices.

Last updated by matthewS-8 5 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#1186826

Tell us what you are trying to do?
I would like to always display the field 'wpcf-current-price' (numeric format) as money format ($xxx,xxxx)

Is there any documentation that you are following?
Yes, reviewed these:
https://toolset.com/forums/topic/display-numeric-value-as-currency/

but not sure how to isolate the function just to this field globally....
I'm currently in the process of changing the price values from strings to numbers (i.e, manually removing dollar signs and commas)

#1186858

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthew,

Thank you for contacting our support forum.

The solution in the ticket was implemented as a shortcode. So instead of using the default types shortcode you will use this custom shortcode.

function format_my_number($atts)
{
    $atts = shortcode_atts(
        array(
        'num' => '',
        'sym' => '',
    ),
        $atts
    );
    $num = $atts['num'];
 
    return $atts['sym'].' '.number_format($num, 0, '.', ',');
}
add_shortcode('format-currency', 'format_my_number');

So it would be [format-currency num="types shortcode goes here" sym="$"]

Please let me know if this helps.
Thanks,
Shane

#1186877
Screen Shot 2019-01-18 at 10.31.05 AM.png
Screen Shot 2019-01-18 at 10.27.30 AM.png
Screen Shot 2019-01-18 at 10.27.26 AM.png

Hi Shane,
I'm getting a php error after adding this to my functions.php file.

Your PHP code changes were rolled back due to an error on line 170 of file wp-content/themes/total-child-theme/functions.php. Please fix and try saving again.

syntax error, unexpected '&'

Also, not sure where to place the short code to get the results? If I put it on the Content Template that this view is using, I get this result.

#1186948

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthew,

I see the issue. You code snippet is being replaced with the ASCII equivalent for some of the items.

Try copy and pasting from here.
https://pastebin.com/UJsXMdtL

#1186953
Screen Shot 2019-01-18 at 1.04.37 PM.png

Thanks Shane, that fixed the php error.
And I've registered both functions under View>View Settings > Third party Shortcode Arguments

Now how do I wrap my entry in this shortcode in a view ?
This gives me no results:
<div class="homePrice">[format-currency]{!{types field='wpcf-current-price'}!}{!{/types}!}[/format-currency]</div>

And this displays "[format_my_number1539900"
<div class="homePrice">[format_my_number]{!{types field='current-price'}!}{!{/types}!}[/format_myNumber]</div>

#1186960

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthew,

The shortcode is actually used like this.

[format-currency num="[types field='wpcf-current-price'][/types]" sym="$"]

Thanks,
Shane

#1186965

Thanks Shane,

I found that I needed to remove the 'wpcf', and that did the trick!!

Now I can simply wrap it in my CSS class of "homePrice" for styling.

<div class="homePrice">[format-currency num="[types field='current-price'][/types]" sym="$"]</div>

Thank-you for your help.

#1186968

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthew,

Happy I was able to assist you.

If there are no further issues on this then you can proceed to marking this issue as resolved.

Thanks,
Shane

#1186972

My issue is resolved now. Thank you!