2016-08-07 26 views
1

我試圖通過短代碼在自定義頁面上顯示產品的價格。如何使用正確逗號在ID中顯示Woocommerce產品價格?

我發現這個線程:How to display Woocommerce product price by ID number on a custom page?

使用此代碼:

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'); 

簡碼:[woocommerce_price id="99"]

我實現的代碼,並得到它現在的工作的唯一的事是價格沒有正確顯示,它沒有註冊逗號。

比如我有當應顯示爲$1,371.43

多數民衆贊成被顯示爲 $13564.34

當它應該$1371.43

顯示爲$13,564.34

它也做同樣的價格

回答

0

函數number_format()可能會有所幫助。例如:

1578.47將改變爲:1,578.47後

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

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

+0

我試圖植入'number_format' ,但沒有得到運氣。 我將'$ html'傳遞給'$ english_format_number = number_format($ html);' 這個數字仍然提醒了同樣的事情。 – Angel

+0

這樣做:'$ html =「price =」。 number_format($ _ product-> get_price(),2);' – JustRayz

+0

是的,我做了類似的事情。謝謝! – Angel

0

得到它現在的工作。

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"] 
相關問題