Замените переменный ценовой диапазон продуктов WooCommerce на «До» и максимальную цену - PullRequest
0 голосов
/ 02 января 2019

Я нашел следующий код (из здесь ), который позволяет мне отображать продукт с переменной ценой на WooCommerce: ' От: 10 ' (от 10 до £ 50 продукт). Я хотел бы эффективно изменить это и показать « До: £ 50 ».

Я пытался настроить код ниже, но просто не могу понять:

function custom_variable_price_range( $price_html, $product ) {
    $prefix = sprintf('%s: ', __('From', 'woocommerce') );

    $min_regular_price = $product->get_variation_regular_price( 'min', true );
    $min_sale_price    = $product->get_variation_sale_price( 'min', true );
    $max_price         = $product->get_variation_price( 'max', true );
    $min_price         = $product->get_variation_price( 'min', true );

    $price_html = ( $min_sale_price == $min_regular_price ) ? wc_price( $min_regular_price ) :
    '<del>' . wc_price( $min_regular_price ) . '</del>' . '<ins>' . wc_price( $min_sale_price ) . '</ins>';

    return ( $min_price == $max_price ) ? $price_html : sprintf( '%s%s', $prefix, $price_html );
}
add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price_range', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_range', 10, 2 );

Вот как бы я хотел, чтобы это выглядело:

Example

Любая помощь приветствуется.

1 Ответ

0 голосов
/ 02 января 2019

В следующем коде будет указана измененная цена «max» с суффиксом «До» и обработкой в ​​диапазоне цен продажи:

add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price_range', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_range', 10, 2 );
function custom_variable_price_range( $price_html, $product ) {

    $prefix = __('Up to', 'woocommerce');

    $max_regular_price = $product->get_variation_regular_price( 'max', true );
    $max_sale_price    = $product->get_variation_sale_price( 'max', true );
    $max_active_price  = $product->get_variation_price( 'max', true );
    $min_active_price  = $product->get_variation_price( 'min', true );

    $price_html = ( $max_sale_price == $max_regular_price ) ? wc_price( $max_active_price ) :
    '<del>' . wc_price( $max_regular_price ) . '</del> <ins>' . wc_price( $max_sale_price ) . '</ins>';

    return $min_active_price == $max_active_price ? $price_html : sprintf('%s %s', $prefix, $price_html);
}

Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Проверено и работает.


Если вы не хотите обрабатывать диапазон цен продажи, вместо этого вы будете использовать:

add_filter( 'woocommerce_variable_sale_price_html', 'custom_variable_price_range', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_range', 10, 2 );
function custom_variable_price_range( $price_html, $product ) {

    $prefix = __('Up to', 'woocommerce');

    $max_active_price  = $product->get_variation_price( 'max', true );
    $min_active_price  = $product->get_variation_price( 'min', true );

    $price_html = wc_price( $max_active_price );

    return $min_active_price == $max_active_price ? $price_html : sprintf('%s %s', $prefix, $price_html );
}

Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Проверено и работает.

...