首页 > 解决方案 > Woocommerce 单一变体价格显示不符合预期

问题描述

在我们的 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 文件中使用的过滤器。

这是我的问题的屏幕截图: 在此处输入图像描述

我希望范围变化价格与销售价格和带删除线的常规价格一起显示,但在您选择变化后只显示一个价格。

标签: phpwordpresswoocommerce

解决方案


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

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


推荐阅读