How do I make a specific payment gateway to be free shipping on woocommerce How do I make a specific payment gateway to be free shipping on woocommerce wordpress wordpress

How do I make a specific payment gateway to be free shipping on woocommerce


WooCommerce requires that shipping is selected before a gateway, this is how COD works because it checks if an enabled shipping method is selected before providing COD as an option. So if the COD method does not work for you then there is no other way to accomplish this because you are asking the checkout process work backwards from how it was designed.

You can not modify shipping once a payment gateway has been selected due to the way WooCommerce works. You can only add extra fees within the code for each gateway.


After a while of thinking about it, I got curious and thought i'd have a play to see if it was truely impossible. It turns out that with a bit of crude hacking you can actually get this to work, here is a basic plugin that will accomplish the task:

<?php/** * Plugin Name: Free Shipping For BACS * Description: Makes shipping for BACS free. * Version: 0.0.1 * Author: Kodaloid * Requires at least: 4.4 * Tested up to: 4.8 */if (!defined('ABSPATH')) exit;add_action('init', 'fg_init');function fg_init() {  add_action('woocommerce_cart_calculate_fees', 'fg_add_fee');  add_action('wp_footer', 'fg_footer', 9999);}function fg_footer() {  ?>  <script type="text/javascript">    jQuery(function($) {      setInterval(function() {        $(".input-radio[name='payment_method']").off().change(function() {        console.log('triggered');          jQuery('body').trigger('update_checkout');        });      }, 500);    });  </script>  <?php}function fg_add_fee($the_cart) {  global $woocommerce;  if ($woocommerce->session->chosen_payment_method == 'bacs') {    $woocommerce->cart->add_fee('Free Shipping For BACS', -($the_cart->shipping_total), true, 'standard');  }}

Save the code above as free_shipping_for_bacs.php and install the plugin using the Upload Plugin feature in WordPress.

Basically what this does is check the session to see which payment method has been picked, then if the bacs method is chosen adds a fee which is minus the total of the shipping. This works but because the cart updates using AJAX you need to trigger the update_checkout event attached to the body in JavaScript every time the payment method changes in order to see the change reflected in the cart above.

So as a hack I have added a loop that re-adds the change handler every 500ms to the footer event (if your theme does not implement wp_footer hook, make sure to add it), this can and should be improved upon if you decide to use this code as there are better methods to check if the change event needs re-adding, it's just I don't have a lot of time today.

Koda