【问题标题】:Display the default variation price and savings amount on Woocommerce 3在 Woocommerce 3 上显示默认变化价格和节省金额
【发布时间】:2018-10-18 11:23:11
【问题描述】:

我需要在我的 Woocommerce 主页和类别页面

上显示默认变化价格以及常规价格和节省金额

我在此链接答案中找到了以下代码 Display lowest variation price and discounted percentage in WooCommerce

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 ) {

    // Main Price
    $regular_price = $product->is_type('variable') ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price();
    $sale_price = $product->is_type('variable') ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price();


    if ( $regular_price !== $sale_price && $product->is_on_sale()) {
    // Percentage calculation and text
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = __(' Save', 'woocommerce' ).' '.$percentage;

    $price = '<del>' . wc_price($regular_price) . '</del> <ins>' . wc_price($sale_price) . $percentage_txt . '</ins>';
    }
    return $price;
}

现在此代码在我的网站上完美运行,但此代码显示“最低”变化的价格和节省的百分比,我需要显示“默认”变化的价格和实际节省的金额。

提前谢谢你。

【问题讨论】:

    标签: php woocommerce product price variations


    【解决方案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 = isset($default_variaton) ? $default_variaton['display_price']: $product->get_variation_regular_price( 'min', true );
            $sale_price = isset($default_variaton) ? $default_variaton['display_regular_price']: $product->get_variation_sale_price( 'min', 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 ).'%';
            $percentage_txt = __(' Save', 'woocommerce' ).' '.$percentage;
    
            $price = '<del>' . wc_price($regular_price) . '</del> <ins>' . wc_price($sale_price) . $percentage_txt . '</ins>';
        }
        return $price;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    现在此代码将永远无法获得所选的变体价格,因为它是客户端浏览器上的实时事件,处理起来要复杂得多(在这种情况下将需要 Php 和 Jquery)。

    相关链接答案:Display lowest variation price and discounted percentage in WooCommerce

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2019-09-10
      • 2021-06-24
      • 2019-03-04
      • 2016-05-09
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多