Skip Navigation

[Resolved] Format a number field as a price

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

Problem:
How to format a Types number field as a price with currency?

Solution:
You would need to create a custom shortcode to transform the Types field value using the PHP function money_format.

The client shared the solution for their particular requirement below: https://toolset.com/forums/topic/transforming-content-in-number-field-as-price/#post-611859

Relevant Documentation:
https://developer.wordpress.org/plugins/shortcodes/
https://developer.wordpress.org/reference/functions/get_post_meta/
http://php.net/manual/en/function.money-format.php

100% of people find this useful.

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

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 7 replies, has 4 voices.

Last updated by Gerard 6 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#606174

Hi there,

I have created a toolset number field. This includes a price (integer => 4852).
Now it is necessary to format this number as a price of the product (=> 4.852,- Euro).
How can I realize this?

Best regards
Uwe

#606304

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Uwe

To format a number as a current you need to use either JavaScript or PHP.

Which you use depends on if you are more comfortable with one or the other, but also where you intend to output the price.

I would think the best option would be to create a custom shortcode where you pass the field name as an attribute, and the PHP function gets the value of that field for the current post, then uses money_format to convert it into a currency format string which is then returned to the shortcode.

https://developer.wordpress.org/plugins/shortcodes/
https://developer.wordpress.org/reference/functions/get_post_meta/
(hidden link)

If you get stuck, let me know.

#611859

Hi Nigel,

Thank you very much for your support!
It helped me and I realized it this way, working without a shortcode-plugin:

functions.php:

function format_money( $atts ) {
	extract( shortcode_atts( array(
	'price' => '0'), $atts) );
	$money = number_format($price, 0, '', '.').',− €';
	return $money;
}
add_shortcode('format_money', 'format_money');

In my views and pages:

[format_money price="[types field='preis' format='FIELD_VALUE'][/types]"]

#623419

Hi Nigel,

your solution worked perfect. But since a couple of time the shortcodes doesn´t work correctly.
As a college of you suggested, I changed all curly brackets into square brackets.

That works generally, but not for formatted price fields.

This works:

[format_money price='4860']

This does not work:

[format_money price='[types field='preis' format='FIELD_VALUE'][/types]']

This does not work:

[format_money price='{!{types field='preis' format='FIELD_VALUE'}!}{!{/types}!}']

The Website uses the theme DIVI (Elegantthemes).
Could you please provide a solution?

Thank you very much and best regards
Uwe

#623652

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Uwe

I added this to a Content Template designed with Divi in a text module, and it worked correctly:

<p>Price: [format_money price="[types field='price' output='raw'][/types]"]</p>

Where are you trying to use the shortcode?

#623716
divi_textfield_formatted_price.png

Hi Nigel,

I try to use the shortcode in a DIVI Text-Module, too.
=> Screenshot

#1572673

Thanks so much for this code! The code below worked on my site for US dollars.

Code I am using in Toolset -> Views -> [pick my view] -> Loop Editor:

$[format_money price="[types field='price' output='raw'][/types]"]

Code I am using in my (child) theme's functions.php file:

//* Shortcode to format the price (add thousands comma separator)
add_shortcode('format_money', 'sk_format_money');
function sk_format_money( $atts ) {
    extract( shortcode_atts( array( 'price' => '0' ), $atts ) );
    // $money = number_format( ( float )$price, 2, '.', ',' ); // if you want to show cents
    $money = number_format( $price );
    return $money;
}

Notes:
- My price field is a Number custom field
- My prices are entered without dollar signs or commas (i.e. 500000 and 40000 and 300 and 70)
- In Toolset -> Settings -> Front-end Content -> Third-party shortcode arguments -> Shortcodes registered manually, I added

format-money
#1604997

Thank You @uweh