【问题标题】:Woocommerce single variation price displaying not as intendedWoocommerce 单一变体价格显示不符合预期
【发布时间】:2019-07-18 17:44:59
【问题描述】:

在我们的 woocommerce 网站上,我们的价格范围是从最低价到最高价。例如 2 美元到 600 美元。然后我们对整个范围有 10% 的折扣,因此它将显示为整个范围 $2 - $600 的删除线,并显示为 $1.80 - $540。

当我现在选择一个变体时,它会显示 $540 - 例如 $0。但我只想显示 $540 并且 - $0 必须删除。我将展示一个截图示例。

我尝试了各种过滤器,但单个变体价格的实际输出没有改变。

add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );

function custom_price_format( $price, $product ) {

    // 1. Variable products
    if( $product->is_type('variable') ){

        // Searching for the default variation
        $default_attributes = $product->get_default_attributes();
        // Loop through available variations
        foreach($product->get_available_variations() as $variation){
            $found = true; // Initializing
            // Loop through variation attributes
            foreach( $variation['attributes'] as $key => $value ){
                $taxonomy = str_replace( 'attribute_', '', $key );
                // Searching for a matching variation as default
                if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){
                    $found = false;
                    break;
                }
            }
            // When it's found we set it and we stop the main loop
            if( $found ) {
                $default_variaton = $variation;
                break;
            } // If not we continue
            else {
                continue;
            }
        }
        // Get the default variation prices or if not set the variable product min prices
        $regular_price = $product->get_variation_regular_price( 'min', true );
        $sale_price = $product->get_variation_sale_price( 'min', true );
        $regular_price_max = $product->get_variation_regular_price('max', true);
        $sale_price_max = $product->get_variation_sale_price( 'max', true );
    }
    // 2. Other products types
    else {
        $regular_price = $product->get_regular_price();
        $sale_price    = $product->get_sale_price();
    }

    // Formatting the price
    if ( $regular_price !== $sale_price && $product->is_on_sale()) {
        // Percentage calculation and text
        $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
        $txt = " - ";

        $price = '<del>'.wc_price($regular_price).'</del><ins>'.$txt.'</ins><del>'.wc_price($regular_price_max).'</del><br><ins>'.wc_price($sale_price).$txt.wc_price($sale_price_max).'</ins>';
    }
    return $price;
}

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;
}

这是我目前在我的 functions.php 文件中使用的过滤器。

这是我的问题的屏幕截图:

我希望范围变体价格与促销价一起显示,正常价格带有删除线,但在您选择变体后只显示一个价格。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:
    add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
    
    function custom_price_format( $price, $product ) {
    
    // 1. Variable products
    if( $product->is_type('variable') ){
    
        // Searching for the default variation
        $default_attributes = $product->get_default_attributes();
        // Loop through available variations
        foreach($product->get_available_variations() as $variation){
            $found = true; // Initializing
            // Loop through variation attributes
            foreach( $variation['attributes'] as $key => $value ){
                $taxonomy = str_replace( 'attribute_', '', $key );
                // Searching for a matching variation as default
                if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){
                    $found = false;
                    break;
                }
            }
            // When it's found we set it and we stop the main loop
            if( $found ) {
                $default_variaton = $variation;
                break;
            } // If not we continue
            else {
                continue;
            }
        }
        // Get the default variation prices or if not set the variable product min prices
        $regular_price = $product->get_variation_regular_price( 'min', true );
        $sale_price = $product->get_variation_sale_price( 'min', true );
        $regular_price_max = $product->get_variation_regular_price('max', true);
        $sale_price_max = $product->get_variation_sale_price( 'max', true );
    }
    // 2. Other products types
    else {
        $regular_price = $product->get_regular_price();
        $sale_price    = $product->get_sale_price();
    }
    
    // Formatting the price
    if ( $regular_price !== $sale_price && $product->is_on_sale()) {
        // Percentage calculation and text
        $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
        $txt = " - ";
    
        //Check if Sale Price Exists
        if(wc_price($sale_price_max) !== 0.00){
          $price = '<del>'.wc_price($regular_price).'</del><ins>'.$txt.'</ins><del>'.wc_price($regular_price_max).'</del><br><ins>'.wc_price($sale_price).$txt.wc_price($sale_price_max).'</ins>';
        } else {
        //else return price without second sale price
        $price = '<del>'.wc_price($regular_price).'</del><ins>'.$txt.'</ins><del>'.wc_price($regular_price_max).'</del><br><ins>'.wc_price($sale_price).'</ins>';
        }    
    }
    return $price;
    }
    

    请尝试上面的代码并告诉我。

    【讨论】:

    • 您好,这可行,但实际价格范围位于顶部。
    • imgur.com/a/fFoW3ou我添加了一个截图,我不确定你更改了什么代码,所以我不想对你编辑的过滤器进行太多修改。
    • 您是否只想隐藏价格 == 0.00 还是一直隐藏?
    • 嗨,是的,很抱歉,这实际上是有效的,需要一段时间才能显示正确的价格。所以它仍然显示为 $300 - 例如 $0 然后它会在不久后显示正确的 $300 价格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2022-01-03
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    相关资源
    最近更新 更多