【问题标题】:How to display Woocommerce product price by ID with correct Commas?如何使用正确的逗号按 ID 显示 Woocommerce 产品价格?
【发布时间】:2016-12-13 12:28:22
【问题描述】:

我试图通过短代码在自定义页面上显示产品的价格。

我找到了这个帖子: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"]

我已经实现了代码并让它工作了,现在唯一的问题是价格没有正确显示它没有注册逗号。

例如,我的价格显示为$13564.34

什么时候应该显示为$13,564.34

$1371.43 也是如此

什么时候应该显示为$1,371.43

【问题讨论】:

    标签: php wordpress woocommerce price


    【解决方案1】:

    函数 number_format() 可能会有所帮助。例如:

    1578.47 将更改为:1,578.47 之后

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

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

    【讨论】:

    • 我尝试植入number_format 但没有成功。我是通过$html’ into ‘$english_format_number = number_format($html);这个号码还是一样的提醒。
    • 这样做:$html = "price = " . number_format($_product->get_price(), 2);
    • 是的,我做了类似的事情。谢谢!
    【解决方案2】:

    现在可以使用了。

    代码:

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

    短代码:

    [woocommerce_price id="99"]
    

    【讨论】:

      猜你喜欢
      • 2018-04-23
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多