Answer the question
In order to leave comments, you need to log in
How to make it so that when choosing a form of payment, for example, the client chose paypal, the order amount increased by 14%?
I found this code, only it increases the order amount for a specific country, but I don’t know how to get the payment method that the client chose, and then you can make a comparison, if paupal, then the order amount increases by 14%, and if, for example, cash on delivery, then the amount remains the same, please help
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$county = array('US');
$percentage = 0.01;
if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
endif;
}
Answer the question
In order to leave comments, you need to log in
The author, this function does the calculation only in the basket. In order to get the selected payment method, you need to cling to the hooks related to the checkout (i.e. checkout).
Alternatively, you can use this hook woocommerce_after_calculate_totals
. An example of use is as follows:
add_action( 'woocommerce_after_calculate_totals', 'custom_fee_for_paypal' );
function custom_fee_for_paypal( $cart ) {
//проверяем это страница оформления заказа или нет
if ( is_checkout() || defined('WOOCOMMERCE_CHECKOUT') ) {
//получаем выбранный метод оплаты
$patment_method = WC()->session->get( 'chosen_payment_method' );
//Если выбранный метод равен paypal то идём дальше
if( $patment_method == 'paypal' ) {
$percentage = 0.14;
//Получаем значение суммы надбавки за пайпел
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percentage;
//Добавляем надбавку
$cart->add_fee( 'Комиссия за PayPal ', $surcharge, true, '' );
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question