Skip Navigation

[Closed] Format WooCommerce price field with 2 decimals

This support ticket is created 2 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 – 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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 2 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#2011541

Hello,

Tell us what you are trying to do?
I would like to have the price field displayed properly. Currently it's showing as 4.2 instead of 4.20
Using this shortcode, [types field='views_woo_price'][/types]

What is the link to your site?
hidden link

#2011677

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To convert the format of the values stored in the custom fields, for showing them on the front-end, you can create a custom shortcode, as explained in this guide:
https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/

For example:


add_shortcode('convert-decimal-places', 'convert_decimal_places_func');
function convert_decimal_places_func($atts) {
 
    $a = shortcode_atts( array(
        'number' => '',
        'places' => '2',
    ), $atts );
 
    if(!empty($a['number']) && !empty($a['places'])) {
        $new = number_format((float)$a['number'], $a['places'], '.', '');
        return $new;
    }
     
}

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 new shortcode accepts two attributes "number" and "places", and you can use it with your custom field's value, like this:


[convert-decimal-places number="[types field='views_woo_price'][/types]" places="2"]

As a result, the custom shortcode will ensure that the shortcode's output consists of a number of 2 decimal places.

regards,
Waqar

#2012497
Screen Shot 2021-04-06 at 8.57.51 AM.png

Hi Waqar,

Thank you for replying. The shortcode isn't working as described. I'm getting code returned instead of the number correctly formatted, [/types]' places='2']

#2013555

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

I've performed some tests on my website with the same code, but couldn't reproduce this.

Can you please share temporary admin login details, along with the link to a page where this shortcode can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

The topic ‘[Closed] Format WooCommerce price field with 2 decimals’ is closed to new replies.