php - 为在 WooCommerce 中显示自定义产品价格的功能启用回价后缀

标签 php wordpress woocommerce hook-woocommerce price

我使用"Enable sale price for logged users and regular price for unlogged users in Woocommerce "第一个功能代码,隐藏销售价格。这样我就可以使用登录用户的销售价格和常规价格与未登录用户的价格,这就像一个魅力。

我遇到的问题是,它还从设置中删除了 woocommerce_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 文件中。经过测试并有效。

关于php - 为在 WooCommerce 中显示自定义产品价格的功能启用回价后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56023673/

相关文章:

php - Mysql 查询不起作用?

css - Wordpress 网站上的多个内容背景?

php - 以编程方式将 'images' 和缩略图添加到 WordPress 帖子

php - 如何将产品信息传递给 woocommerce 插件?

PHP "Simple"查询需要大量时间

php - 使用 Laravel 将数组数据存储在数据库中

php - 脚本执行时如何获取部分输出?

php - 我需要使用 themeMyLogin 小部件

php - 根据 Woocommerce 中的特定用户角色显示菜单项

html - CSS显示错误