用向后价格后缀到在WooCommerce中显示自定义产品价格的功

用向后价格后缀到在WooCommerce中显示自定义产品价格的功

本文介绍了启用向后价格后缀到在WooCommerce中显示自定义产品价格的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用"" ,该功能代码可隐藏销售价格.这样,我可以使用已登录用户的销售价格和常规价格与未登录的用户进行对比,它的工作原理很吸引人.

I am using "Enable sale price for logged users and regular price for unlogged users in Woocommerce" first function code, that hides sales price. That way I can use sales price and regular price for logged in vs none logged in users and it Works like a charm.

我遇到的问题是,它也从设置中杀死了woocommerce_price_suffix字段,我想将其包含在函数中,但不知道如何.

The Problem I have is that it also kills the woocommerce_price_suffix field from the settings and I want to include it in my function, but don't know how.

推荐答案

以下内容将添加价格后缀:

The following will add back the price suffix:

//Variable and simple product displayed prices (removing sale price range)
add_filter( 'woocommerce_get_price_html', 'custom_get_price_html', 20, 2 );
function custom_get_price_html( $price, $product ) {
    if( $product->is_type('variable') )
    {
        if( is_user_logged_in() ){
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('max') ) );
        } else {
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('max') ) );
        }

        if( $price_min != $price_max ){
            if( $price_min == 0 && $price_max > 0 )
                $price = wc_price( $price_max );
            elseif( $price_min > 0 && $price_max == 0 )
                $price = wc_price( $price_min );
            else
                $price = wc_format_price_range( $price_min, $price_max );
        } else {
            if( $price_min > 0 )
                $price = wc_price( $price_min);
        }
        $price .= $product->get_price_suffix()
    }
    elseif( $product->is_type('simple') )
    {
        if( is_user_logged_in() )
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
        else
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );

        if( $active_price > 0 )
            $price = wc_price($active_price) . $product->get_price_suffix();
    }
    return $price;
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于启用向后价格后缀到在WooCommerce中显示自定义产品价格的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:06