php - Woocommerce 返回价格未正确显示

标签 php mysql wordpress function woocommerce

我有非常简单的 woocommerce 代码来显示扣除后的产品价格。请参阅下面的代码。

add_action( 'woocommerce_get_price_html' , 'rp_get_price' );
function rp_get_price($price){
    $rp_percentage = 10;
    $regular_price = get_post_meta( get_the_ID(), '_price', true );
    $rp_discount = $regular_price * $rp_percentage / 100;
    $price = $regular_price - $rp_discount; 
    return $price;
}

add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
    $rp_percentage = 10;

    $min_price = $product->get_variation_price( 'min', true );
    $max_price = $product->get_variation_price( 'max', true );

    $rp_min_discount = $min_price*10/100;
    $rp_min_price = $min_price - $rp_min_discount;

    $rp_max_discount = $max_price*10/100;
    $rp_max_price = $max_price - $rp_max_discount;          

    if ($min_price != $max_price)
    {
        $price = $rp_min_price . '-' . $rp_max_price;
    }
    else
    {
        $price = $rp_min_price;
    }

    return $price;
}

对于单一产品,价格显示正确。对于具有相同最小和最大费率的变体产品,显示正确,但我的问题是对于变体价格从低价格到高价格,它没有显示为“20 -30”(它只显示最低价格) 您能帮我解决一下吗?

最佳答案

试试这个代码:

    add_action( 'woocommerce_get_price_html' , 'rp_get_price' );

    function rp_get_price($price){
        global $product;

        //get the sale price of the product whether it be simple, grouped or variable
        $sale_price = get_post_meta( get_the_ID(), '_price', true);

        //get the regular price of the product, but of a simple product
        $regular_price = get_post_meta( get_the_ID(), '_regular_price', true);

        $rp_percentage = 10;

        if( $product->is_type( 'simple' ) ):

            $rp_discount = $regular_price * $rp_percentage / 100;
            $price = $regular_price - $rp_discount;

        else:

            if ($regular_price == ""){

            $min_price = $product->get_variation_price( 'min', true );
            $max_price = $product->get_variation_price( 'max', true );

                    $rp_min_discount = $min_price*10/100;
                    $rp_min_price = $min_price - $rp_min_discount;

                    $rp_max_discount = $max_price*10/100;
                    $rp_max_price = $max_price - $rp_max_discount;          


        if ($min_price != $max_price)
        {
        $price = $rp_min_price . '-' . $rp_max_price;
        }
        else
        {
            $price = $rp_min_price;
        }
    }
     endif;

    return $price;
    }

编辑:

    add_action( 'woocommerce_get_price_html' , 'rp_get_price' );

    function rp_get_price($price){
        global $product;

        //get the sale price of the product whether it be simple, grouped or variable
        $sale_price = get_post_meta( get_the_ID(), '_price', true);

        //get the regular price of the product, but of a simple product
        $regular_price = get_post_meta( get_the_ID(), '_regular_price', true);

        $rp_percentage = 10;

        if ($regular_price == ""){
            $currency = get_woocommerce_currency_symbol();

            $min_price = $product->min_variation_price; 

            $max_price = $product->max_variation_price;

                $rp_min_discount = $min_price*10/100; 
                $rp_min_price = $min_price - $rp_min_discount;

                $rp_max_discount = $max_price*10/100;
                $rp_max_price = $max_price - $rp_max_discount;          


            if ($min_price != $max_price)
            {
                $price = $rp_min_price . '-' . $rp_max_price;
            }
            else
            {
                $price = $rp_min_price;
            }
        }
        else{
            $rp_discount = $regular_price * $rp_percentage / 100;
            $price = $regular_price - $rp_discount;
        }
    return $price;
    }

尝试新编辑的代码,因为旧代码不符合标准。

关于php - Woocommerce 返回价格未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27957846/

相关文章:

php - 如何根据受欢迎程度在在线商店订购商品

wordpress - 在Google Container Engine和kubernetes中管理Wordpress文件

mysql - 使用 mysql 查询 WordPress 查找并替换图像链接中的 åäö 字母

php - 在MySql php中将数据存储到带有外键的2个表中

php - 带有附加列的 SQL 数据透视表

php - PHP 中无休止的查询/循环?

php - 长 SQL 查询优化

php - 如何存储 MYSQL 过程的结果,以便我可以在另一个使用 PHP 的过程中使用它

MySQL 主键自增

javascript - 向下滚动时,粘性标题过渡变得跳跃和丑陋