我在写一篇woocommerce每个产品的一些细节(短/长描述)。
我想插入,显示目前的价格(销售/定期)的说明里面SHORTCODE。它应该是类似的东西在后端:
“现在买它,因为只有[wc_price] $”
有没有简码,我可以使用是什么?
在这里你去:在您的function.php添加此代码
function short_code_woo_comm_desc( $atts ) {
$atts = shortcode_atts( array(
'id' => null
), $atts, 'tag_for_short_code_price' );
if ( empty( $atts[ 'id' ] ) ) {
return '';
}
$product = wc_get_product( $atts['id'] );
if ( ! $product ) {
return '';
}
return $product->get_price_html();
}
add_shortcode( 'tag_for_short_code_price', 'short_code_woo_comm_desc' );
采用:
[tag_for_short_code_price id="101"]
这是一个最简单的片段,你想要做什么,而不需要手动输入ID:
function my_shortcode_product_price() {
$html = '';
global $product;
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) );
$args = array(
'ex_tax_label' => false,
'currency' => 'USD',
'decimal_separator' => '.',
'thousand_separator' => ' ',
'decimals' => 2,
'price_format' => '%2$s %1$s',
);
$html = "<span>" . wc_price( $price, $args ) . "</span>";
return $html;
}
add_shortcode( 'product_price', 'my_shortcode_product_price' );
上面的代码放在你的活动主题的functions.php
文件。之后,你可以使用这样的短代码:
[product_price]