Изменить отображаемую сумму доставки в WooCommerce - PullRequest
2 голосов
/ 19 апреля 2019

Мне нужно изменить стоимость доставки программно:

<?php
   $percentage = 50;
   $current_shipping_cost = WC()->cart->get_cart_shipping_total();
   echo $current_shipping_cost * $percentage / 100;
?>

К сожалению, это не работает, и я всегда получаю 0 (ноль) .

Как изменить отображаемую общую сумму доставки на общую сумму, основанную на рассчитанном проценте скидки?

1 Ответ

1 голос
/ 19 апреля 2019

Ниже будет отображаться общий объем доставки в процентах. Есть 2 способа:

1) Первый способ с пользовательской функцией.

В файле function.php вашей активной дочерней темы (или активной темы):

function wc_display_cart_shipping_total( $percentage = 100 )
{
    $cart  = WC()->cart;
    $total = __( 'Free!', 'woocommerce' );

    if ( 0 < $cart->get_shipping_total() ) {
        if ( $cart->display_prices_including_tax() ) {
            $total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );
            if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
                $total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $total = wc_price( $cart->shipping_total * $percentage / 100  );
            if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
                $total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat()  . '</small>';
            }
        }
    }
    return  $totals;
}

Использование:

<?php echo wc_display_cart_shipping_total(50); ?>

2) Второй способ с крючком фильтра.

В файле function.php вашей активной дочерней темы (или активной темы):

add_filter( 'woocommerce_cart_shipping_total', 'woocommerce_cart_shipping_total_filter_callback', 11, 2 );
function woocommerce_cart_shipping_total_filter_callback( $total, $cart )
{
    // HERE set the percentage
    $percentage = 50;

    if ( 0 < $cart->get_shipping_total() ) {
        if ( $cart->display_prices_including_tax() ) {
            $total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );
            if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
                $total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $total = wc_price( $cart->shipping_total * $percentage / 100  );
            if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
                $total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat()  . '</small>';
            }
        }
    }
    return  $totals;
}

Использование:

<?php echo WC()->cart->get_cart_shipping_total(); ?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...