【问题标题】:Display discount percentage after the selected variation sale price in WooCommerce在 WooCommerce 中选择变体销售价格后显示折扣百分比
【发布时间】:2018-05-11 20:37:34
【问题描述】:

下面的代码显示了用户选择变体后选择的变体价格

add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
function filter_show_variation_price( $condition, $product, $variation ){
    if( $variation->get_price() === "" ) return false;
    else return true;
}

我需要计算将显示的促销价格与正常价格之间的折扣百分比。但仅在选择变体之后,而不是之前(因为我将通过 css 删除之前显示的价格)。

我认为this answer 接近但还没有。

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    全部 以下代码仅适用于单个产品页面上的可变产品。

    我之前制作的链接答案中的第二个代码运行良好。我做了一些小改动,只针对可变产品。

    我添加了一个额外的挂钩函数,该函数将删除仅可变产品标题下显示的价格。

    这里是所有需要的代码:

    // Always Display the selected variation price for variable products (already working)
    add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
    function filter_show_variation_price( $condition, $product, $variation ){
        if( $variation->get_price() === "" ) return false;
        else return true;
    }
    
    // Remove the displayed price from variable products in single product pages only
    add_action( 'woocommerce_single_product_summary', 'remove_the_displayed_price_from_variable_products', 9 );
    function remove_the_displayed_price_from_variable_products() {
        global $product;
    
        // Just for variable products
        if( ! $product->is_type('variable') ) return;
    
        // Remove the displayed price from variable products
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    }
    
    // Display the selected variation discounted price with the discounted percentage for variable products
    add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
    function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
        global $product;
    
        // Just for variable products on single product pages
        if( $product->is_type('variable') && is_product() ) {
        
            // Getting the clean numeric prices (without html and currency)
            $regular_price = floatval( strip_tags($regular_price) );
            $sale_price = floatval( strip_tags($sale_price) );
        
            // Percentage calculation and text
            $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
            $percentage_txt = __(' Save ', 'woocommerce' ).$percentage;
        
            return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>';
        }
        return $price;
    }
    

    代码进入活动子主题(或主题)的functions.php文件或任何插件文件中。

    此代码经过测试,大部分仅适用于 WooCommerce 版本 3+

    【讨论】:

    • 你好@LoicTheAztec 它几乎做到了!计算很完美,但现在目录页面上的所有价格都显示为零。
    • 我认为这是因为我所有的产品都是可变的,它们都有促销价格,你发送的链接的产品都不符合这个条件。
    • 它工作得几乎完美,但页面底部的相关产品会话中所有产品的值都为零。
    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    相关资源
    最近更新 更多