Skip Navigation

[Resolved] numerical field formatting

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.

This topic contains 5 replies, has 2 voices.

Last updated by Clay 4 years, 3 months ago.

Assigned support staff: Minesh.

Author
Posts
#508059
Screen Shot 2017-03-30 at 9.56.20 PM.png

I am trying to: display the woocommerce quantity in stock

I visited this URL: hidden link

I expected to see: whole numbers like "12" or "0"

Instead, I got: 12.000000 and 0.000000

#508139

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you please tell me which shortcode you are using to display the quantity field?

#508283

Shortcode for stock quantity is [wpv-post-field name="_stock"]

I also would like to format another field as price in US dollars, like "$16.00" instead of just "16". That shortcode is [types field='views_woo_price'][/types].

Thanks for your help!

#508291

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please try to add following code to your current theme's functions.php file:

function func_format_num($atts, $content = "") {

$x = number_format(wpv_do_shortcode($content),2, '.', ',');
if($atts['type']=='qty') {
     $val = explode(".",$x);
     return  $val[0];
}else if($atts['type']=='price'){
     return  "$ ".$x ;
}
}
add_shortcode('format_num', 'func_format_num');

To display quantity:

[format_num type='qty'] [wpv-post-field name="_stock"] [/format_num]

To display price:

[format_num type='price'] [types field='views_woo_price'][/types] [/format_num]
#508399

Thanks Minesh. That seems to do what I wanted. The only problem now is that a few of the products do not display any price, even though their prices are shown on the individual product pages. Strange. If you go to hidden link you will see the second product has no price, and there are others also.

#508419

Never mind. I figured it out. Thanks for your help!