Php 在WooCommerce中按产品ID显示带有快捷码的产品价格
在WooCommerce中,我使用此代码通过一个短代码显示定义产品ID中的产品价格。但它并不能真正满足我的需要。这是代码:Php 在WooCommerce中按产品ID显示带有快捷码的产品价格,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,在WooCommerce中,我使用此代码通过一个短代码显示定义产品ID中的产品价格。但它并不能真正满足我的需要。这是代码: function so_30165014_price_shortcode_callback( $atts ) { $atts = shortcode_atts( array( 'id' => null, ), $atts, 'bartag' ); $html = ''; if( intval( $atts['id'] ) > 0 && fun
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\u price()
…它可以工作,但我想显示的价格是这样的:
所以划掉了正常价格和旁边的销售价格,就像这个屏幕截图。如果没有销售价格,则只显示正常价格
我还有其他一些问题:
- 我需要用
,显示价格,而不是$
,因此我用以下代码将货币符号从$
(美元)替换为$
(欧元):€
$html=“€”$数量代码>
- 我需要在价格后显示货币符号,例如:
(中间有空格),而不是37€
$37
wc\u price()
。您还需要获得销售价格
若要在有销售价格或没有销售价格时使其正常工作,请尝试以下代码(注释):
此代码经过测试并正常工作。您将获得以下信息:
好的,我的一个朋友帮了我,给了我很好的代码。 如果它能帮助别人。。。 这是:
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' );
函数so\u 30165014\u price\u shortcode\u回调($atts){
$atts=shortcode_atts(数组('id'=>null,),$atts,'bartag');
$html='';
如果(intval($atts['id'])>0&&function_存在('wc_get_product')){
$_product=wc_get_product($atts['id']);
$price=$\产品->获取\价格();
$regular\u price=$\u product->get\u regular\u price();
$price_format=number_format($_product->get_price(),2',',',');
$regular_price_format=number_format($_product->get_regular_price(),2',',',');
如果($price!=$regular\u price){
$html.=''.$常规价格格式。“€”;
}
$html.=''.$price_格式。“€”;
}
返回$html;
}
添加快捷代码(“woocommerce\u price”、“so\u 30165014\u price\u快捷代码\u callback”);
再见 如果任何人需要价格的简单文本输出,另一种选择是:
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\u price id=XX]
(将XX替换为产品id)
要将样式添加到价格中,请通过添加行来添加跨度类:
$html.=''$价格。"";代码>,
更改行返回$price
到返回$html代码>,
然后根据需要将样式格式添加到css中
(注意,这只输出当前价格,不是正常价格。)@LoicTheAztec非常感谢您的代码
我做了一些修改,所以我在这里分享。也许它对某些人也有用:)
注意:此版本不是“即插即用”。换句话说:如果你想让它适应你的需要,它需要一些工作。
此版本还根据页面语言更改货币。例如:
- 如果页面为英文,则价格以英镑显示
- 如果页面为西班牙语,则价格以欧元显示
注:本版本还包括:
- 删除css。相反,它添加了一个类。我这样做是因为我已经在那个类中定义了我的样式
- 在折扣价格中添加链接以结帐。因此,如果页面语言为fr,则结果URL将为:
代码:
结果:
在我输入此代码之前,Woocommerce的所有设置都已正确设置。但它没有显示我想要的。可能是因为代码不适合我使用。但是我看不到,因为我不知道php coding.oups,我没有看到你的回复。非常抱歉!非常感谢你的评论,我能理解这段代码的意思。谢谢。请更新代码,使其接受现行货币,而不是默认货币。我正在使用多电流开关,无法解决这个问题,否则,您的功能工作得很好。@Mike这段代码不是多电流开关插件的maid…所以您必须问一个新问题…非常好的答案。你能不能把它更新一下,这样价格就包括税了?稍微更正一下:要添加样式,行应该是:$html.=''$价格。""; (“”,而不是“”)否则代码段插件也将解析错误。快捷码应为[product_price id=“XX”]。简单,工作正常。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'] );
$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' );
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' );
/**
* 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' );
.my-standard-price{
font-size:200%;
}
.my-discount-price{
font-size:150%;
}