IN WooCommerce我使用此的代码胎面用短代码以显示产品的价格从定义的产品ID.但它并没有真正做到我想要的.这是代码:
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' );
Run Code Online (Sandbox Code Playgroud)
我对PHP编码知之甚少.但我已经看到有这个其他线程来显示产品价格:
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Run Code Online (Sandbox Code Playgroud)
我试图将这些代码混合到大代码中,并替换get_price()
......它可以工作,但我想要的是显示价格是这样的:
所以正常价格划掉了,旁边的促销价就像在这个截图中一样.如果没有促销价,则仅显示正常价格.
我还有一些其他问题:
我需要显示价格€
,而不是$
,因此我已使用以下代码将货币符号从$
(美元)替换为€
(欧元) …