Woocommerce仅显示折扣产品的一种价格 [英] Woocommerce Show only one price for variable product on discount

查看:75
本文介绍了Woocommerce仅显示折扣产品的一种价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的WooCommerce网站:sweetworldcandy.com

Here is my WooCommerce website: sweetworldcandy.com

问题是,在可变产品价格中,最小和最大值显示我想要的是,如果该产品不在销售中,则显示最小的价值,如果该产品在销售中,则通过添加一个斜线作为删除标记来显示最小的价值和最小的要约价格

The issue is, in variable product price the least and the max value is showing what I want is if the product is not on sale show the least value if it is on sale show the least value and the least value of offer price by adding a slash as delete tag

我在下面分享了这三张图片以供参考:

I shared this three images below for reference:

推荐答案

2020更新 (适用于Woocommerce 3 +)

从Woocommerce 3+开始,由 wc_price()替换了已弃用的功能 woocommerce_price():

Replaced deprecated function woocommerce_price() by wc_price() since Woocommerce 3+:

add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
    $variation_min_reg_price = $product->get_variation_regular_price('min', true);
    $variation_min_sale_price = $product->get_variation_sale_price('min', true);
    if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
        if ( !empty($variation_min_sale_price) )
            $price = '<del class="strike">' .  wc_price($variation_min_reg_price) . '</del>
        <ins class="highlight">' .  wc_price($variation_min_sale_price) . '</ins>';
    } else {
        if(!empty($variation_min_reg_price))
            $price = '<ins class="highlight">'.wc_price( $variation_min_reg_price ).'</ins>';
        else
            $price = '<ins class="highlight">'.wc_price( $product->regular_price ).'</ins>';
    }
    return $price;
}


原始线程(在WooCommerce 3之前):下面的代码应该可以实现您所期望的:


Original thread (before WooCommerce 3): The code below should do what you expect:

add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
    $variation_min_reg_price = $product->get_variation_regular_price('min', true);
    $variation_min_sale_price = $product->get_variation_sale_price('min', true);
    if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
        if ( !empty($variation_min_sale_price) )
            $price = '<del class="strike">' .  woocommerce_price($variation_min_reg_price) . '</del>
        <ins class="highlight">' .  woocommerce_price($variation_min_sale_price) . '</ins>';
    } else {
        if(!empty($variation_min_reg_price))
            $price = '<ins class="highlight">'.woocommerce_price( $variation_min_reg_price ).'</ins>';
        else
            $price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
    }
    return $price;
}

该代码会出现在您活动的子主题(或主题)的function.php文件中,或者出现在任何插件文件中.

此代码已经过测试并且可以正常工作.

This code is tested and works.

这篇关于Woocommerce仅显示折扣产品的一种价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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