[英]Display product prices with a shortcode by product ID in WooCommerce
在 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' );
我对php编码知之甚少。 但我已经看到有另一个线程来显示产品价格:
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
我尝试将这些代码混合到大代码中,并替换get_price()
......它有效,但我想要显示价格是这样的:
所以常规价格和旁边的销售价格被划掉,就像这个截图一样。 如果没有销售价格,则仅显示正常价格。
我还有一些其他问题:
我需要显示价格是€
,而不是$
,所以我用以下代码将货币符号从$
(美元)替换为€
(欧元) : $html = "€" . $number;
$html = "€" . $number;
我需要在价格之后显示货币符号,例如: 37 €
(中间有空格),而不是$37
。
我怎样才能让它以干净的正常方式工作?
已更新(考虑您的价格是否显示含税或不含税)
使用 Woocommerce 已经有可以在代码中使用的格式化价格函数wc_price()
。 您还需要获得销售价格……
要在有销售价格或没有销售价格时使其工作,请尝试以下代码(注释) :
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' ) ){
// Get an instance of the WC_Product object
$product = wc_get_product( intval( $atts['id'] ) );
// Get the product prices
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
// Your price CSS styles
$style1 = 'style="font-size:40px;color:#e79a99;font-weight:bold;"';
$style2 = 'style="font-size:25px;color:#e79a99"';
// Formatting price settings (for the wc_price() function)
$args = array(
'ex_tax_label' => false,
'currency' => 'EUR',
'decimal_separator' => '.',
'thousand_separator' => ' ',
'decimals' => 2,
'price_format' => '%2$s %1$s',
);
// Formatting html output
if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
$html = "<del $style2>" . wc_price( $regular_price, $args ) . "</del> <ins $style1>" . wc_price( $sale_price, $args ) . "</ins>"; // Sale price is set
else
$html = "<ins $style1>" . wc_price( $price, $args ) . "</ins>"; // No sale price set
}
return $html;
}
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
代码在您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。
用法(例如产品 ID 37
) :
[product_price id="37"]
此代码经过测试并且可以工作。 你会得到这个:
如果有人需要简单的价格文本输出,另一种选择是:
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 .= "<span class="price_by_id_shortcode">". $price . "</span>";
,
更改行return $price;
return $html;
,
然后根据需要将您的样式格式添加到您的 CSS 中。
(注意这仅输出当前价格,而不是正常价格。)
好的,我的一个朋友帮助了我,并给了我很好的代码。 如果它帮助别人......这里是:
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 .= "<span style='font-size:25px;text-decoration:line-through;'>".$regular_price_format." €"."</span>";
}
$html .= "<span style='font-size:40px;font-weight:bold;'> ". $price_format." €"."</span>";
}
return $html;
}
add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );
再见 !
@LoicTheAztec 非常感谢您的代码!!!
我做了一些修改,所以我在这里分享它。 也许它对某人也有用:)
注意:此版本不是“即插即用”。 换句话说:如果你想让它适应你的需要,它需要一些工作。
此版本还根据页面语言更改货币。 例如:
注意:这个版本还有:
代码:
/**
* Shortcode to display default price and discounted price
* USAGE:
* [product_price id="37"]
* Original code: https://stackoverflow.com/a/47237674/1198404
* Modificated code to fit our needs: https://stackoverflow.com/a/63418231/1198404
*/
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' ) ){
// Get an instance of the WC_Product object
$product = wc_get_product( intval( $atts['id'] ) );
// Get the product prices
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
// Build checkout URL like: https://www.edinventa.com/fr/checkout/?fill_cart=7982&
$domain_base_url = get_bloginfo('wpurl');
$wpml_language_code = ICL_LANGUAGE_CODE;
$checkout_url = $domain_base_url . "/" . $wpml_language_code . "/checkout/?fill_cart=" . $atts['id'];
// Get currency symbol
$currency_symbol = get_woocommerce_currency();
// Your price CSS styles
$style1 = 'class="my-discount-price" style="outline: none; text-decoration: none;"';
$style2 = 'class="my-standard-price" style="text-decoration: line-through;"';
// Formatting price settings (for the wc_price() function)
$args = array(
'ex_tax_label' => false,
'currency' => '',
'decimal_separator' => wc_get_price_decimal_separator(),
'thousand_separator' => wc_get_price_thousand_separator(),
'decimals' => wc_get_price_decimals(),
'price_format' => get_woocommerce_price_format(),
);
// Formatting html output with discount price with link like: <a href="https://www.ed...;" ins class="xxx"> 49€</a>
if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
$html = "<del $style2>" . wc_price( $regular_price, $args ) . "</del> <a href=$checkout_url ins $style1>" . wc_price( $sale_price, $args ) . "</ins>"; // Sale price is set
else
$html = "<a href=$checkout_url ins $style1>" . wc_price( $sale_price, $args ) . "€</ins>"; // No sale price set (UNTESTED)
}
return $html;
}
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
CSS 代码:
.my-standard-price{
font-size:200%;
}
.my-discount-price{
font-size:150%;
}
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.