1

I was trying to display the price of a product on a custom page through a short code.

I found this thread: How to display Woocommerce product price by ID number on a custom page?

With this CODE:

function so_30165014_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
    'id' => null,
), $atts, 'bartag' );

$html = '';

if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
     $_product = wc_get_product( $atts['id'] );
     $html = "price = " . $_product->get_price();
}
return $html;
}
add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );

Shortcode: [woocommerce_price id="99"]

I've implemented the code and got it working the only thing now is that the price is not being displayed right it's not registering the commas.

For example I have a price that's being displayed as $13564.34

When it should be displayed as $13,564.34

It's also doing the same for $1371.43

When it should be displayed as $1,371.43

Community
  • 1
  • 1
Angel
  • 23
  • 5

2 Answers2

0

The function number_format() may help. For example:

1578.47 would change to: 1,578.47 after

number_format(1578.47, 2, '.', ',');

http://php.net/manual/en/function.number-format.php

  • I tried implanting the `number_format` But got no luck. I was passing the `$html’ into ‘$english_format_number = number_format($html);` The number still reminds the same. – Angel Aug 07 '16 at 18:58
  • do this: `$html = "price = " . number_format($_product->get_price(), 2);` –  Aug 07 '16 at 19:35
  • Yeah I did something similar to that. Thanks! – Angel Aug 07 '16 at 19:40
0

GOT IT WORKING NOW.

CODE:

function so_30165014_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'bartag' );

$html = '';

if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
 $_product = wc_get_product( $atts['id'] );
 $number = number_format($_product->get_price(), 2, '.', ',');
 $html = "$" . $number;

 }
 return $html;
 }
 add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );

SHORTCODE:

[woocommerce_price id="99"]
Angel
  • 23
  • 5