在Woocommerce 3上显示默认变动价格和节省金额 [英] Display the default variation price and savings amount on Woocommerce 3

查看:95
本文介绍了在Woocommerce 3上显示默认变动价格和节省金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示默认变化价格&正常价格我的 Woocommerce主页&上的节省金额类别页面

I need to display default variation price & with regular price & savings amount on my Woocommerce Homepage & Category Page

我在此链接上找到了以下代码答案 在WooCommerce中显示最低的变化价格和折扣百分比

I found following code on this links answer 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;
}

现在此代码可在我的网站上完美运行,但此代码显示最低"变体的价格并节省了百分比,因此我需要显示默认"变体的价格&实际节省金额.

Now this code works perfectly on my site but this code shows price for 'Lowest' variation and savings in percentage where I need to show the price for 'Default' variation & Actual saving amount.

先谢谢您.

推荐答案

尝试以下操作,我们首先在其中搜索可变产品(默认变动价格).

Try the following, where we search first for variable products, the default variation prices.

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文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

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

Now this code will never get the selected variation price as it's a live event on client browser that is much more complicated to handle (Php and Jquery will be needed in this case).

相关的链接的答案:显示最低的变化WooCommerce中的价格和折扣百分比

这篇关于在Woocommerce 3上显示默认变动价格和节省金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆