Can't get Woocommerce Payment Gateway Fees to be added upon checkout

22 views Asked by At

I use WooCommerce combined with the WooCommerce Stripe Gateway.

I wish to add an extra fee when paying with Credit Card (Stripe), but I can't get it to work.

I have tried Booster for Woocommerce, where I have added a payment gateway fee using their "Gateways Fees and Discounts" sub-plugin. I have also tried to implement two codes in the Code Snippets plugin, one for the detection of the Payment Gateway selection:

function custom_detect_stripe_payment_method() {
    if (is_checkout()) { // Ensures this only runs on the checkout page
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                $('form.checkout').on('change', 'input[name="payment_method"]', function() {
                    var paymentMethod = $('input[name="payment_method"]:checked').val();
                    if (paymentMethod === 'stripe') { // Adjust if your Stripe ID differs
                        $.ajax({
                            url: wc_checkout_params.ajax_url,
                            type: 'POST',
                            data: {
                                action: 'detect_stripe_payment_method', // Custom backend hook
                            },
                            success: function(response) {
                                console.log('Stripe selected, session updated.');
                            }
                        });
                    }
                });
            });
        </script>
        <?php
    }
}
add_action('wp_footer', 'custom_detect_stripe_payment_method');

and one for applying the fee:

add_action('wp_ajax_detect_stripe_payment_method', 'set_stripe_session_flag');
add_action('wp_ajax_nopriv_detect_stripe_payment_method', 'set_stripe_session_flag');

function set_stripe_session_flag() {
    WC()->session->set('chosen_payment_method_stripe', true);
    wp_send_json_success();
}

add_action('woocommerce_cart_calculate_fees', 'add_stripe_fee', 20);

function add_stripe_fee() {
    if (WC()->session->get('chosen_payment_method_stripe')) {
        $subtotal = WC()->cart->subtotal;
        $fee = $subtotal * 0.03; // Calculate 3% of the subtotal as the fee
        WC()->cart->add_fee(__('Stripe Processing Fee', 'woocommerce'), $fee, true);
    }
}

So, I have tried to use these two codes in Code Snippets, and I have tried the Booster plugin. None of them works, even though I am in troubleshoot mode with a default theme and only the three plugins activated.

I have noted that when I click the Credit Card gateway upon checkout, the page doesn't seem to dynamically update (by fading and then dynamically updating). Nothin really happens.

I have cleared all cache etc. between my tests.

Please help me troubleshoot this issue as it's a serious problem for my business.

0

There are 0 answers