在 WooCommerce 中显示最低变化价格和折扣百分比

Display lowest variation price and discounted percentage in WooCommerce

基于此 post :

我一直试图在我的网站上到处显示可变和非可变产品的最低变化价格和相关折扣百分比。

这是我当前使用的代码:

add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format', 10, 2 );


function bbloomer_variation_price_format( $price, $product ) {

    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice ) {
        $price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
    }
    return $price;
}

add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    // Getting the clean numeric prices (without html and currency)
    $regular_price = floatval( strip_tags($regular_price) );
    $sale_price = floatval( strip_tags($sale_price) );

    // Percentage calculation and text
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = __(' Save ', 'woocommerce' ).$percentage;

    return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>';
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'mmx_remove_select_text');

如果我只保留第二个功能,它适用于所有产品,但不适用于可变产品,可变产品在商店页面上显示价格范围。如果我添加第一个函数,它会显示最低产品变体的价格而不是价格范围,但旁边没有折扣百分比。

我可以将第二个函数的 % 代码添加到第一个函数中,但这很麻烦,有没有办法合并这些函数,这样我就可以在所有地方显示折扣百分比,并在最低变化旁边显示什么时候是可变产品?

这可以通过稍微改变将替换两个挂钩函数的第一个函数来完成:

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

此代码进入您的活动子主题(或主题)的 function.php 文件。

已测试并适用于您在之前 questions/answer 中所做的自定义设置。