我写了这个函数,只是在 checkout 页面上增加了9%的额外费用.
现在,我想按ID排除一些产品,使其不会在一个函数中增加额外费用.
如何在对代码进行最小更改的情况下简单地做到这一点?
add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway' );
function add_checkout_fee_for_gateway() {
// Check if we are on the checkout page
if ( is_checkout() ) {
global $woocommerce;
$chosen_gateway = $woocommerce->session->chosen_payment_method;
if ( $chosen_gateway == 'paypal' ) {
$percentage = 0.09;
/* for all products prices + shipping (total price)
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
*/
$surcharge = ( $woocommerce->cart->get_subtotal() ) * $percentage;
$woocommerce->cart->add_fee( '9% value added tax', $surcharge, true, '' );
}
}
}