skip to Main Content

I am using WooCommerce Subscriptions with manual payments using the ‘bacs’ payment method. When recurring orders are created, the code below changes the status from ‘Pending’ to ‘Processing’ which keeps the subscription ‘Active’. This allows a recurring order to be automatically created every interval regardless of whether the previous order has been marked ‘Completed’ or not.

add_action('wcs_renewal_order_created', 'sab_auto_complete_renewals_by_payment_method', 10, 2);
function sab_auto_complete_renewals_by_payment_method($order_id)
{
  if ( ! $order_id ) {
        return;
  }
  
  $order = wc_get_order( $order_id );

  if ($order->data['status'] == 'pending') {
        $payment_method=$order->get_payment_method();
        if ($payment_method!="bacs")
        {
            $order->update_status( 'processing' );
        }
  }
}

However, although this works, I am seeing errors logged within WooCommerce.

Error: "scheduled action 26701 (subscription payment) failed to finish processing due to the following error: Uncaught Error: Call to a member function get_total() on bool in /home/sites/13b/6/69781a941d/public_html/contract/sab-content/plugins/woocommerce-subscriptions/includes/class-wc-subscriptions-manager.php:128"

Line 128 (class-wc-subscriptions-manager.php):

if ( 0 == $renewal_order->get_total() ) {
                $renewal_order->payment_complete(); // We don't need to reactivate the subscription here because calling payment complete on the order will do that for us.
            } else {

                if ( $subscription->is_manual() ) {
                    do_action( 'woocommerce_generated_manual_renewal_order', wcs_get_objects_property( $renewal_order, 'id' ), $subscription );
                    $renewal_order->add_order_note( __( 'Manual renewal order awaiting customer payment.', 'woocommerce-subscriptions' ) );
                } else {
                    $renewal_order->set_payment_method( wc_get_payment_gateway_by_order( $subscription ) ); // We need to pass the payment gateway instance to be compatible with WC < 3.0, only WC 3.0+ supports passing the string name

                    if ( is_callable( array( $renewal_order, 'save' ) ) ) { // WC 3.0+ We need to save the payment method.
                        $renewal_order->save();
                    }
                }
            }
        } else {
            $renewal_order = false;
        }

If anyone knows how to prevent this error, any help would be appreciated!

Or if anyone knows how to keep a subscription ‘Active’ regardless of the order status, please let me know. Ive tried a variety of suggestions, however I am unable to find the right hook for renewals: Auto change Woocommerce Subscriptions status to "On-Hold" rather than "Active"

2

Answers


  1. Chosen as BEST ANSWER

    If anyone needs to know how I did this without errors, I eventually used the following simplified code which can change the renewal order status to any status while setting the subscription status as active (or any other status). We are using this to process manual subscription payments regardless of whether payments are successful or not.

    add_filter('wcs_renewal_order_created', 'change_order_and_subscription_status', 10, 2);
    function change_order_and_subscription_status($renewal_order, $subscription) {
        $renewal_order->update_status('processing'); // Set the renewal order status
        $subscription->update_status('active'); // Set the subscription status   
        return $renewal_order;
    }
    

  2. From what I see here, the hook is getting the order object and subscription object rather than the ID so you should use the order as is rather than global $product;
    $order = wc_get_order( $order_id ); (also, not sure what that global $product is all about)

    P.S. I am working on the exact same problem :))
    P.P.S I made the same mistake and you and forgot to return the order from the function I added which caused the same error

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search