2
votes

I want users to have a free checkout for my subscription service if they have 100% discount coupon code for the product. So if cart total gets equal to zero, then there should be no payment gateway and users should be able to signup directly.

I think WooCommerce don't allow working without payment gateway or can be difficult to modify, so I want to change the current payment gateway to "Cash on Delivery" if cart total equals to 0 . I shall change Cash on delivery text to something else later.

Here is link to my dummy development. Use Couponcode: abcd1234 Here is the function that I'm using to hide payment gateways in my functions.php file :

add_filter( 'woocommerce_available_payment_gateways', 'paypal_100' );
function paypal_100($available_gateways) {
      if ( WC()->cart->total == '0' ) {
          unset( $available_gateways);
      }
return $available_gateways;
}

Thanks in advance, Yash

2

2 Answers

4
votes

Well, after 6 hours of R & D here is the solution.

I made a minor change in plugins\woocommerce-subscriptions\classes\class-wc-subscriptions-cart.php And everything is working as I wanted. No payment method is required for subscription products if cart total equal to 0.

Before code (Line - 280 ):

public static function cart_needs_payment( $needs_payment, $cart ) {

    if ( self::cart_contains_subscription() ) {

        $is_for_one_period = ( self::get_cart_subscription_length() > 0 && self::get_cart_subscription_length() == self::get_cart_subscription_interval() ) ? true : false;

        if ( $cart->total == 0 && false === $needs_payment && $cart->recurring_total > 0 && false === $is_for_one_period && 'yes' !== get_option( WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no' ) ) {
            $needs_payment = true;
        }
    }

After Code:

public static function cart_needs_payment( $needs_payment, $cart ) {

    if ( self::cart_contains_subscription() ) {

        $is_for_one_period = ( self::get_cart_subscription_length() > 0 && self::get_cart_subscription_length() == self::get_cart_subscription_interval() ) ? true : false;

        if ( $cart->total == 0 && false === $needs_payment && $cart->recurring_total > 0 && false === $is_for_one_period && 'yes' !== get_option( WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no' ) ) {
            $needs_payment = false;
        }
    }

I just changed "$needs_payment = true;" to "$needs_payment = false;"

0
votes

You can use the following code snippet. So we check if the a specific pay method is available and check if the total amount in the shopping cart is equal to some amount. Then we can hide that payment method. In your case, you can hide all available methods.

function payment_gateway_disable_total_amount( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['paypal'] ) && $woocommerce->cart->total == 0 ) {
    unset(  $available_gateways['paypal'] );
}
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_total_amount' );