Изменить функцию wc_cart_totals_shipping_method_label в Woocommerce - PullRequest
1 голос
/ 02 апреля 2019

Внутри плагина Woocommerce в подпапке includes есть файл wc-cart-functions.php.
Я хотел бы изменить функцию wc_cart_totals_shipping_method_label(), но мне не разрешено копировать эту функцию в functions.php моей темы. Я считаю, что я должен использовать пользовательское действие / фильтр, чтобы изменить эту основную функцию, но не знаю, как это сделать.

Исходная функция:

function wc_cart_totals_shipping_method_label( $method ) {
    $label     = $method->get_label();
    $has_cost  = 0 < $method->cost;
    $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );

    if ( $has_cost && ! $hide_cost ) {
        if ( WC()->cart->display_prices_including_tax() ) {
            $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
            if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $label .= ': ' . wc_price( $method->cost );
            if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        }
    }

    return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}

Какой должна быть функция:

function wc_cart_totals_shipping_method_label( $method ) {
    $label     = $method->get_label();
    $has_cost  = 0 < $method->cost;
    $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );

    if ( $has_cost && ! $hide_cost ) {
        if ( WC()->cart->display_prices_including_tax() ) {
            $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
            if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $label .= ': ' . wc_price( $method->cost );
            if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
                $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        }
    }
    /* Here's the code I want added */
    elseif ( ! $has_cost && ! $hide_cost ) {
        $label .= ': Free';
    }

    return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}

Что я хочу сделать: Методы доставки, которые имеют нулевую стоимость, должны иметь «Свободный», добавленный к их ярлыку. Прямо сейчас ничего не показано рядом с названием метода

Как я могу внести необходимые изменения в эту функцию, не перезаписывая основные файлы Woocommerce?

1 Ответ

3 голосов
/ 02 апреля 2019

Как вы можете видеть в этой функции, вы можете использовать woocommerce_cart_shipping_method_full_label фильтр-ловушку, чтобы управлять этим изменением следующим образом:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_cart_shipping_method_full_label', 10, 2 );
function change_cart_shipping_method_full_label( $label, $method ) {
    $has_cost  = 0 < $method->cost;
    $hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );

    if ( ! $has_cost && ! $hide_cost ) {
        $label  = $method->get_label() . ': Free';
    }
    return $label;
}

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...