X
    Categories: WooCommerce Tutorials, Tips and GuidesWordPress Tutorials, Tips and Guides

WooCommerce Subscription Status On Hold

It is easy to setup subscription payments on WooCommerce stores. In many cases, subscription payments set up recurring payments that could be collected at preset intervals.

WooCommerce Subscription activates when the order changes to Processing (and above). This is ideal for subscription based payment models but not so great for Cash on Delivery (CoD) payment option.

In the case of CoD, the order status moves directly to Processing. This means that the buyer would have access to the product even before the payment is cleared. This often proves to be a challenge for ecommerce stores that operate on subscription model.

In many cases, store owners need to put this behavior on hold so that they could collect the payment before product is delivered. Fortunately, WooCommerce offers a simple way of implementing this behavior.

Put the following code snippet into the functions.php, which is located in the theme folder.

function cw_suspend_subscription( $order ) 
{
  if ( ! is_object( $order ) ) {
  $order = new WC_Order( $order );

}

   foreach ( WC_Subscriptions_Order::get_recurring_items( $order ) as $order_item ) {

   $cw_key_subs = WC_Subscriptions_Manager::get_subscription_key( $order->ID, WC_Subscriptions_Order::get_items_product_id( $order_item ) );

   $payment_gateways = WC()->payment_gateways->payment_gateways();

   $payment_gateway  = isset( $payment_gateways[ $order->recurring_payment_method ] ) ? $payment_gateways[ $order->recurring_payment_method ] : '';

   if ( $payment_gateway->ID == 'cod' && $order->get_status() != 'completed' ) {

   WC_Subscriptions_Manager::put_subscription_on_hold( $order->user_id, $cw_key_subs );
   }
   }
}
add_action( 'updated_users_subscriptions_for_order', 'cw_suspend_subscription' );

Code Explanation

The code snippet starts with checking the payment gateway. If the CoD option is selected, it further checks the order status. If the order status is not Completed, the code changes the subscription status to On Hold.

Because of this change in the subscription status, the customer will not be able to use the product/service before the payment is received. The good thing is that there is no need to manually change the subscription status once the payment has been received. Once the status of the order changes to Completed, the subscription’s status changes to Active.

Conclusion

In this tutorial, I covered the scenario where the subscription status affects product delivery. By using a simple code snippet, the store owners could modify the status of the order to ensure that payments have been received before product delivery.if you need any help with the idea, do leave a comment below.

Owais Alam: is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com