WooCommerce可变产品:仅保留“分钟"带自定义标签的价格 [英] WooCommerce variable products: keep only "min" price with a custom label

查看:119
本文介绍了WooCommerce可变产品:仅保留“分钟"带自定义标签的价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在功能文件中,我添加了一个过滤器挂钩,以便在变动产品最低"价格之前添加自定义标签.

In the functions file I have added a filter hook to add a custom label before the variation product "min" price.

如何获得与价格相同的标签?

How can I get the label in the same line as the price?

在下面查看我的代码和屏幕截图:

See my code and the screenshot below:

add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
    $min_price = $product->get_variation_price( 'min', true );
    $price = sprintf( __( 'From%1$s', 'woocommerce' ), wc_price( $min_price ) );
    return $price;
}

推荐答案

自WooCommerce 3起, woocommerce_variable_sale_price_html 钩子已被弃用,不再有用.如果您不在乎最低"销售价格(正在销售最低价格),则可以使用以下方法:

Since WooCommerce 3, woocommerce_variable_sale_price_html hook is deprecated and not anymore useful. If you don't care about "min" sale price (when the min price is on sale), you can use this:

add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
function custom_min_max_variable_price_html( $price, $product ) {
    $prices = $product->get_variation_prices( true );
    $min_price = current( $prices['price'] );

    $min_price_html = wc_price( $min_price ) . $product->get_price_suffix();
    $price = sprintf( __( 'From %1$s', 'woocommerce' ), $min_price_html );

    return $price;
}

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

经过测试,可在WooCommerce 3+上使用.您将获得如下内容:

Tested and works on WooCommerce 3+. You will get something like this:

如果您关注最低"销售价格(当最低价格开始销售时),并且您想同时显示两个价格,则应改用以下代码:

If you care about "min" sale price (when the min price is on sale), and you want to display both prices, you should use this code instead:

add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
function custom_min_max_variable_price_html( $price, $product ) {
    $prices = $product->get_variation_prices( true );
    $min_price = current( $prices['price'] );

    $min_keys = current(array_keys( $prices['price'] ));
    $min_price_regular = $prices['regular_price'][$min_keys];
    $min_price_html = wc_price( $min_price ) . $product->get_price_suffix();

    if( $min_price_regular != $min_price ){ // When min price is on sale (Can be removed)
        $min_price_regular_html = '<del>' . wc_price( $min_price_regular ) . $product->get_price_suffix() . '</del>';
        $min_price_html = $min_price_regular_html .'<ins>' . $min_price_html . '</ins>';
    }
    $price = sprintf( __( 'From %1$s', 'woocommerce' ), $min_price_html );

    return $price;
}

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

经过测试,可在WooCommerce 3+上使用.您将获得如下内容:

Tested and works on WooCommerce 3+. You will get something like this:

要处理所有变化价格都相同的情况:

To handle when all variations prices are the same:

WooCommerce可变产品:显示带有不同价格的自定义文本的最低价格

这篇关于WooCommerce可变产品:仅保留“分钟"带自定义标签的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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