Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
3
Indexable
Never
add_action('woocommerce_cart_calculate_fees', 'add_custom_fee');

function add_custom_fee($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    // Getting the chosen payment method
    $chosen_gateway = WC()->session->chosen_payment_method;

    $magic_payment = new WC_Magic_Payment();
    $additional_fee = $magic_payment->get_option('magic-creditcard-additional_fee');

    if ($chosen_gateway == 'your_magic_payment_method_id' && $additional_fee > 0) {
        $percentage = $additional_fee / 100;
        $cart_total = $cart->cart_contents_total;
        $fee = $cart_total * $percentage;
        $cart->add_fee('Växlingsavgift (' . $additional_fee . '%)', $fee, true, '');
    } else {
        // Optionally, remove fee if another payment method is chosen. 
        // If you want this, make sure the name ('Växlingsavgift ...') matches the one you've added.
        foreach ($cart->get_fees() as $fee_key => $fee) {
            if (strpos($fee->name, 'Växlingsavgift') !== false) {
                unset($cart->fees[$fee_key]);
            }
        }
    }
}