【问题标题】:Display the discounted price and percentage on Woocommerce products显示 Woocommerce 产品的折扣价格和百分比
【发布时间】:2018-07-06 22:07:36
【问题描述】:

在下图中,它显示了折扣价和百分比

我没有找到具有此功能的自定义代码搜索。

我正在使用下面的代码来显示折扣价,但是价格没有被格式化(货币符号和小数点都不见了):

add_filter( 'woocommerce_get_price_html', 'modify_woocommerce_get_price_html', 10, 2 );

function modify_woocommerce_get_price_html( $price, $product ) {
    if( $product->is_on_sale() && ! is_admin() )
        return $price . sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $product->regular_price - $product->sale_price );
    else
        return $price;
}

如何显示格式正确的折扣价? 如何同时显示折扣百分比?

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    您的代码有点过时,因为 woocommerce 版本 3 作为 Product 对象属性无法直接访问。相反,您应该使用可用的WC_Product 方法

    要格式化价格,您将使用wc_price() 专用格式化功能。

    现在你可以拥有(3种可能性)

    1) 节省的价格:

    add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
    function change_displayed_sale_price_html( $price, $product ) {
        // Only on sale products on frontend and excluding min/max price on variable products
        if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
            // Get product prices
            $regular_price = (float) $product->get_regular_price(); // Regular price
            $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
    
            // "Saving price" calculation and formatting
            $saving_price = wc_price( $regular_price - $sale_price );
    
            // Append to the formated html price
            $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_price );
        }
        return $price;
    }
    

    2) 节省百分比:

    add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
    function change_displayed_sale_price_html( $price, $product ) {
        // Only on sale products on frontend and excluding min/max price on variable products
        if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
            // Get product prices
            $regular_price = (float) $product->get_regular_price(); // Regular price
            $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
    
            // "Saving Percentage" calculation and formatting
            $precision = 1; // Max number of decimals
            $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
    
            // Append to the formated html price
            $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
        }
        return $price;
    }
    

    3 两者都(折扣价和百分比)

    add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
    function change_displayed_sale_price_html( $price, $product ) {
        // Only on sale products on frontend and excluding min/max price on variable products
        if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
            // Get product prices
            $regular_price = (float) $product->get_regular_price(); // Regular price
            $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
    
            // "Saving price" calculation and formatting
            $saving_price = wc_price( $regular_price - $sale_price );
    
            // "Saving Percentage" calculation and formatting
            $precision = 1; // Max number of decimals
            $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
    
            // Append to the formated html price
            $price .= sprintf( __('<p class="saved-sale">Save: %s <em>(%s)</em></p>', 'woocommerce' ), $saving_price, $saving_percentage );
        }
        return $price;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且有效。

    【讨论】:

    • 哇,感谢您将帖子更正为我想要的描述。感谢我期望的完美答案。没有像这样珍贵的东西,我解决了这个问题。非常感谢,祝你好运。
    • 谢谢你。如何在同一行中添加它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多