在 WooCommerce 中按产品 ID 显示带有短代码的产品价格

问题描述 投票:0回答:4

在 WooCommerce 中,我使用此 tread 的代码通过短代码显示已定义产品 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 编码了解甚少。但我看到有 this other thread 显示产品价格:

$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();

我尝试将这些代码混合到大代码中,并替换为

get_price()
…它有效,但我想要的是显示价格是这样的:

What I want to display

因此,常规价格被划掉,促销价格位于其旁边,如该屏幕截图所示。如果没有促销价,则仅显示正常价。

我还有其他一些问题:

  • 我需要显示价格在

    ,而不是$
    ,所以我用此代码将货币符号从
    $
    (美元)替换为
    (欧元) : $html = "€" . $number;

  • 我需要在价格后面显示货币符号,例如:

    37 €

    (之间有空格),而不是像
    $37

我怎样才能让它以干净的正常方式工作?

php wordpress woocommerce shortcode product-price
4个回答
14
投票

已更新 (考虑您显示的价格是否含税)

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"]

此代码经过测试并且可以工作。你会得到这个:

enter image description here


2
投票

如果有人需要简单的价格文本输出,另一种选择如下:

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 中。

(注意,这仅输出当前价格,而不是常规价格。)


0
投票

好的,我的一个朋友帮助了我,并给了我很好的代码。 如果它能帮助别人... 在这里:

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."&nbsp;€"."</span>";  
      } 
      $html .= "<span style='font-size:40px;font-weight:bold;'>&nbsp;". $price_format."&nbsp;€"."</span>"; 
  } 
  return $html; 
} 

add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );

再见!


0
投票

@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%;
}

结果:

result

© www.soinside.com 2019 - 2024. All rights reserved.