如何使用正确的逗号按 ID 显示 Woocommerce 产品价格? [英] How to display Woocommerce product price by ID with correct Commas?

查看:45
本文介绍了如何使用正确的逗号按 ID 显示 Woocommerce 产品价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我找到了这个帖子:如何在自定义页面上按 ID 号显示 Woocommerce 产品价格?

使用此代码:

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"]

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

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.

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

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

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

它也对 $1371.43

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

推荐答案

现在开始工作.

代码:

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"]

这篇关于如何使用正确的逗号按 ID 显示 Woocommerce 产品价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆