在WooCommerce中,通过产品ID使用一个短代码来显示产品价格。
在WooCommerce中,通过产品ID使用一个短代码来显示产品价格。
在WooCommerce中,我正在使用此链接的代码,通过短代码显示来自定义产品ID的产品价格。但它并没有完全实现我想要的效果。以下是该代码:\n
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' );
\n我对php编码了解有限。但我看到了这个链接,可以显示产品价格:\n
$_product->get_regular_price(); $_product->get_sale_price(); $_product->get_price();
\n我尝试将这些代码混合到大代码中,并替换get_price()
... 它能够工作,但我想要的是以这种方式显示价格:\n所以正常价格上有一个删除线,销售价格紧随其后,就像这个截图中显示的那样。如果没有销售价格,则只显示正常价格。\n此外,我还有一些其他问题:\n
- \n
- 我需要显示的价格以
€
而不是$
,所以我用这段代码将货币符号从$
(美元)替换为€
(欧元):$html = \"€\" . $number;
- 我需要在价格之后显示货币符号,例如:
37 €
(之间有一个空格),而不是$37
。
\n
\n
\n如何以一种干净而正常的方式实现这个?
问题的原因:用户想要在WooCommerce中通过产品ID来显示产品价格,但是没有找到相应的解决方法。
解决方法:用户的朋友提供了一个有效的代码片段来解决这个问题。用户需要将以下代码添加到他们的主题的functions.php文件中:
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'] ); $price = $_product->get_price(); $regular_price = $_product->get_regular_price(); $price_format = number_format($_product->get_price(), 2, '.', ','); $regular_price_format = number_format($_product->get_regular_price(), 2, '.', ','); if($price != $regular_price){ $html .= "".$regular_price_format." €".""; } $html .= " ". $price_format." €".""; } return $html; } add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );
这段代码创建了一个名为`woocommerce_price`的短代码,允许用户通过指定产品ID来显示产品的价格。用户只需要在他们想要显示价格的地方使用`[woocommerce_price id="产品ID"]`的格式,并将`产品ID`替换为实际的产品ID即可。
问题的出现的原因:
在WooCommerce中,默认情况下没有价格的短代码,因此如果用户想要在文章中显示特定产品的价格,他们需要手动编辑每个文章来更新价格。这对于那些写了很多文章并且价格经常变动的人来说是非常繁琐和耗时的。
解决方法:
为了解决这个问题,可以使用以下代码来创建一个短代码,通过产品ID显示产品价格:
function wc_price_by_id_shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => null, ), $atts, 'bartag' ); if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){ $_product = wc_get_product( $atts['id'] ); $price = wc_price( $_product->get_price() ); } return $price; } add_shortcode( 'product_price', 'wc_price_by_id_shortcode' );
然后,在文章中使用以下短代码来显示产品价格:[product_price id=XX]
(将XX替换为产品ID)。
如果想要为价格添加样式,可以在代码中添加以下行:$html .= ''. $price . "";
,并将return $price;
改为return $html;
。然后根据需要,将样式格式添加到CSS中。
这样一来,用户就可以通过在文章中插入短代码并指定产品ID来显示特定产品的价格了。当产品价格变动时,用户只需要更新产品价格,而不需要手动编辑每个文章。
问题的出现原因:在WooCommerce中,需要根据产品ID显示产品价格,但没有直接的方法来实现这一点。
解决方法:可以使用短代码(shortcode)来显示产品价格。下面是一个可以实现这一功能的示例代码:
function custom_price_shortcode_callback( $atts ) { $atts = shortcode_atts( array( 'id' => null, ), $atts, 'product_price' ); $html = ''; if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){ // 获取WC_Product对象的实例 $product = wc_get_product( intval( $atts['id'] ) ); // 获取产品价格 $price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // 获取活动价格 $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // 获取正常价格 $sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // 获取促销价格 // 定义价格的CSS样式 $style1 = 'style="font-size:40px;color:#e79a99;font-weight:bold;"'; $style2 = 'style="font-size:25px;color:#e79a99"'; // 格式化价格设置(用于wc_price()函数) $args = array( 'ex_tax_label' => false, 'currency' => 'EUR', 'decimal_separator' => '.', 'thousand_separator' => ' ', 'decimals' => 2, 'price_format' => '%2$s %1$s', ); // 格式化HTML输出 if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price ) $html = "" . wc_price( $regular_price, $args ) . "" . wc_price( $sale_price, $args ) . ""; // 设置了促销价格 else $html = "" . wc_price( $price, $args ) . ""; // 没有设置促销价格 } return $html; } add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
此代码需要放置在活动子主题(或主题)的`functions.php`文件中,或者放置在任何插件文件中。
使用方法:例如,要显示产品ID为37的产品价格,可以使用以下短代码:
[product_price id="37"]
这段代码经过测试可以正常工作,效果如下图所示:
[](https://i.stack.imgur.com/tLtYh.png)