簡體   English   中英

在 WooCommerce 中按產品 ID 顯示帶有簡碼的產品價格

[英]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."&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' );

再見 !

@LoicTheAztec 非常感謝您的代碼!!!

我做了一些修改,所以我在這里分享它。 也許它對某人也有用:)

注意:此版本不是“即插即用”。 換句話說:如果你想讓它適應你的需要,它需要一些工作。

此版本還根據頁面語言更改貨幣。 例如:

  • 如果頁面為英文,則價格以英鎊顯示。
  • 如果頁面是西班牙語,則價格以歐元顯示。

注意:這個版本還有:

  • 刪除 css。 相反,它添加了一個類。 我這樣做是因為我已經在該類中定義了我的樣式。
  • 在折扣價中添加鏈接以結帳。 因此,如果頁面語言為 fr,則結果 URL 將為: https ://www.edinventa.com/fr/checkout/?fill_cart=7982

代碼:

/**
 * 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM