WooCommerce 可变产品:仅保留带有自定义标签的“最低”价格

问题描述 投票:0回答:1

在函数文件中,我添加了一个过滤器挂钩,以在变体产品“最低”价格之前添加自定义标签。

怎样才能让标签与价格在同一行?

请参阅我的代码和下面的屏幕截图:

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

php wordpress woocommerce product-variations product-price
1个回答
10
投票

自 WooCommerce 3 起,

woocommerce_variable_sale_price_html
钩子已弃用,不再有用。如果您不关心“最低”促销价格(当最低价格促销时),您可以使用这个:

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+ 上测试并运行。你会得到这样的东西:

enter image description here

如果您关心“最低”促销价格(当最低价格促销时),并且您想显示这两个价格,您应该使用以下代码:

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+。你会得到这样的东西:

enter image description here

处理所有变体价格相同的情况:

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

© www.soinside.com 2019 - 2024. All rights reserved.