Skip to content

WooCommerce Subscriptions – Add new subscription action

I have a client that had a requirement to ‘Manually Complete’ WooCommerce subscriptions without having go through the rigmarole of generating a pending renewal request and moving to the orders list and completing it manually.
They’ve already taken the money, they are satisfied that subscriber has paid and they just simply want to log complete that renewal order and activate that subscriber.

Here is how I’ve done it.

Firstly, WooCommerce subscriptions are actually orders so we can add a new order action like so.

add_action('woocommerce_order_actions', 'add_manualcomplete_subscription_action');
function add_manualcomplete_subscription_action($actions) {
 
    global $theorder;
 
    // Make sure we're in a subscription typeof order
    if ( wcs_is_subscription( $theorder ) && ! $theorder->has_status( wcs_get_subscription_ended_statuses() ) ) {
 
        // New action
        $actions['wcs_manually_complete'] = esc_html__( 'Manually complete renewal order', 'woocommerce-subscriptions' );
    }
 
    return $actions;
}

Then I want to do something when that action is triggered. Basically, I want to do all the default subscription stuff but also complete the order that it generates. That will automatically change the subscription status to ‘active’.

add_action( 'woocommerce_order_action_wcs_manually_complete', 'manually_complete_subscription_order');
 
function manually_complete_subscription_order($order) {
 
    // Default subscription status to on-hold
    $order->update_status( 'on-hold' );
 
    // generate a renewal order as normal
    $renewal_order = wcs_create_renewal_order( $order );
 
    // If it's not manual then just log the payment method. 
    // Probably useless in this case really
    if ( ! $order->is_manual() ) {
        $renewal_order->set_payment_method( $order->payment_gateway );
 
    }
 
    // Update the renewal order status to completed
    // This also makes the subscription active
    $renewal_order->update_status('completed');
 
    // Add a custom note for logging
    $order->add_order_note( __( 'Create and complete renewal order requested by admin action.', 'woocommerce-subscriptions' ), false, true );
 
}

About 

10 years + experience in web development working with lots of different technology.

Published inPHPWooCommerceWordPress

5 Comments

  1. davids davids

    unexpected shutdown: PHP Fatal error Call to a member function set_payment_method() on a non-object in httpdocs/wp-content/plugins/woocommerce-subscriptions/includes/class-wc-subscriptions-manager.php on line 110

    After setting next payment form admin.

    • Interesting. I have a site here running with no problems like that at the moment. Care to post the version of the WooCommerce subscriptions?

  2. Is it possible to change the shipping method and cost for this process?

  3. I was wondering if in a similar expect, you would know how to hook an automated manual renewal order to automatically set a subscription to “active” instead of hold? (ie cash and BACS do not activate subscription until the order is manually complete)

    I created a hook below, but it kept giving me an error stating: Cannot redeclare wcs_create_renewal_order() etc…

    Any ideas? I’v spent hours on this!

    // Make subscriptions always active //
    add_filter( 'wcs_create_renewal_order', 'wcs_renewal_order_created' );
    function wcs_renewal_order_created($subscription) {
    $subscription->update_status('completed');
    }
    return $subscription;
    }
    }

    • Madhushanka Madhushanka

      add_filter( ‘wcs_create_renewal_order’, ‘wcs_renewal_order_created’ );
      function wcs_renewal_order_created( $renewal_order, $subscription ) {
      $subscription->update_status( ‘active’ );
      }
      return $renewal_order;
      }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *