在WooCommerce中按产品ID显示产品价格和短代码

时间:2017-11-10 14:17:24

标签: php wordpress woocommerce product price

在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

如何以干净的正常方式使其工作?

4 个答案:

答案 0 :(得分:7)

已更新 (如果您的价格显示有或没有税,则会考虑)

使用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文件中,或者放在任何插件文件中。

USAGE (例如产品ID 37

[product_price id="37"]

此代码已经过测试并且有效。你会得到这个:

enter image description here

答案 1 :(得分:0)

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

grid.getAttribute()
再见!

答案 2 :(得分:0)

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

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](用产品ID替换XX)

要在价格中添加样式,请添加以下行添加span类:

$html .= "<span class="price_by_id_shortcode">". $price . "</span>";

将行return $price;更改为return $html;

然后根据需要将您的样式格式添加到您的CSS中。

(N.B。这只输出当前价格,而不是正常价格。)

答案 3 :(得分: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

相关问题