Как добавить% к общей сумме, но только подряд, а не обновлять общую стоимость - PullRequest
1 голос
/ 01 мая 2020
add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_based_on_cart_total', 10, 1 );
function custom_fee_based_on_cart_total( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // The percentage
    $percent = 15; // 15%
    // The cart total
    $cart_total = $cart->cart_contents_total; 

    // The conditional Calculation
    $fee = $cart_total >= 25 ? $cart_total * $percent / 100 : 0;

    if ( $fee != 0 ) 
        $cart->add_fee( __( "Extra", "woocommerce" ), $fee, false );
}

Я ищу код, подобный следующему:

  • Добавить% к общей корзине, но я не хочу, чтобы он обновлял общую стоимость корзины
  • Я просто хочу добавить дополнительную строку, как сейчас, с "extra"

Пример: вы делаете покупки за 10 долларов

Общая сумма: 10 долларов Дополнительно: 11,5 долларов

Я все еще хочу, чтобы общая сумма была 10 долларов, но просто чтобы показать им, если они заплатят 15% «дополнительно», я вышлю кое-что дополнительное.

1 Ответ

1 голос
/ 01 мая 2020

Поскольку это показано дважды, используйте

  • woocommerce_review_order_before_order_total - страница оформления заказа
  • woocommerce_cart_totals_before_order_total - страница корзины
function my_custom_fee_based_on_cart_total() {
    // optional
    //if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // The percentage
    $percent = 15; // 15%

    // Get cart total
    $cart_total = WC()->cart->get_cart_contents_total();

    // The conditional Calculation
    $fee = $cart_total >= 25 ? $cart_total * $percent / 100 : 0;

    echo '<tr class="my-class"><th>Extra</th><td>' . wc_price($fee) . '</td></tr>';
}
add_action( 'woocommerce_review_order_before_order_total', 'my_custom_fee_based_on_cart_total', 10, 0 );
add_action( 'woocommerce_cart_totals_before_order_total', 'my_custom_fee_based_on_cart_total', 10, 0 ); 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...