Do foreign trade friends know that the use of PayPal payment is required to handle the fee, and WooCommerce default PayPal payment gateway does not set the option of the fee, may be the default foreign payment fee is the recipient to pay?
I'm sure that if you are smart and patient you can find a plugin that supports setting fees for PayPal payments. I don't have that patience, so I researched the code related to the WooCommerce PayPal payment gateway and found that we can add a handling fee for PayPal through the code on both ends.
Code to add a fee to the PayPal payment gateway
Without further ado, let's get to the code (which shows how impatient I am 😄). As usual, just add the code to functions.php.
add_action('woocommerce_cart_calculate_fees', function ()
{
$chosen_gateway = WC()->session->get('chosen_payment_method');
if ($chosen_gateway == 'paypal') {
if ( ! wc_prices_include_tax()) {
$amount = WC()->cart->get_cart_contents_total() + WC()->cart->get_shipping_total();
} else {
$amount = WC()->cart->get_cart_contents_total() + WC()->cart->get_taxes_total() + WC()->cart->get_shipping_total();
}
WC()->cart->add_fee('PayPal Fee', $amount * 0.05);
}
}).
The above code only implements the function to add the handling fee when PayPal payment gateway is selected by default. We need to automatically add this fee when the user switches gateways as well. The code is also very simple.
add_action('woocommerce_review_order_before_payment', function ()
{
? >
(function($) {
$('form.checkout').on('change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script
<?php
}).
The above code translated into human language is: trigger the method to update the shopping cart when the payment method changes.
By the same token, you can also set up fees for other payment gateways, all you need to change is to find the name of the payment gateway, the first code in the name of the gateway can be replaced, to adjust the fee points is even simpler, the code in the 0.05 replaced by the number you need on it.
1 thoughts on “WooCommerce使用PayPal 收款添加收款手续费”
Hello, I have been following your site for a long time can you come up with a tutorial on how to pay for event registration. Not dependent on woocom, looking forward to