phpwordpresswoocommercewoocommerce-subscriptions

Ensure that all Manual Renewal orders with WooCommerce Subscriptions & WooCommerce Payments are automatically 'Completed'


I've built a website using WooCommerce Subscriptions, which uses WooCommerce Payments as the payment gateway. Users have the options to subscribe on either a monthly or an annual basis. All subscriptions come with a free month's trial.

Currently, subscribers are only able to pay using a credit/debit card. For these users, their subscription is temporarily 'on-hold' until the order completes and when the order completes, the subscription returns to the 'active' status.

However, a select few subscribers have been given a 100% discount voucher. For these, they are marked as 'Via Manual Renewal'. When either parent or renewal orders are created, tthey fail to be marked as 'completed'. They are always left as 'pending'. I'm guessing this has something to do with the fact that these are 'Manual Renewal' orders.

I've tried to create some PHP code to add in as a snippet, to catch when these users have orders created, so that they can automatically be marked as 'completed'. This is what I have put together so far.

add_action( 'woocommerce_thankyou', 'sd_woocommerce_auto_complete_order' );
function sd_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }
    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

add_action('woocommerce_subscription_payment_complete', 'subscription_payment_complete_hook_callback', 10, 1);
function subscription_payment_complete_hook_callback( $subscription ) {
    // Get the current order
    $current_order = $subscription->get_last_order( 'all', 'any' );

    // For Paypal recurring orders
    if( $current_order->get_payment_method() === 'woocommerce_payments' ) {

        // Update status to completed
        $current_order->update_status('completed');
    }
}

Unfortunately, this doesn't seem to have an effect, in that I still have to manually mark these users' orders as 'complete'.

Can someone please help me identify the issue with the code I've created, and how I might be able to update it, so that I can ensure all manual renewal orders are marked as 'completed' automatically? Or alternatively, is there a way to change Manual Renewal orders, so that they don't need to constantly generate orders?

Thank you!


Solution

  • So as it turns out, it was a lot simpler than I thought.

    Basically, with WooCommerce, the fact that an order was 'via Manual Renewal' was irrelevant to orders not automatically being marked as 'Completed'.

    Essentially, for an order to be marked as 'Completed' automatically, WooCommerce needs to know that no action needs to be taken on behalf of the site owner/admin, once a payment has been received successfully.

    Therefore, any product created must be marked as both 'Virtual' and 'Downloadable'. My products were marked as 'Virtual' only, which was why it was happening. Now that they're marked as both, all my orders seem to have been marked as 'Completed' automatically now. :)