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
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;"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With