I’m trying to find a way to allow invoices to continue being generated even when the Customer Subscription has an on-hold status using WooCommerce Subscriptions.
The issue I’m encountering is that WooCommerce stops generating invoices once the subscription is put “on hold” after a failed payment (the related order get a failed status).
Sometimes, our team doesn’t notice these subscriptions, and months can go by without invoices being generated, while the customer continues to receive our product.
Since our subscription products are physical, not digital, we need WooCommerce to keep generating invoices and sending emails every month, even when the subscription is on hold.
I’m trying to use this code to enable invoices to be generated even when the subscription is “on hold”.
// Generate invoice even if the subscription is on-hold
add_action('woocommerce_scheduled_subscription_payment', 'generate_invoice_for_on_hold_subscription', 10, 2);
function generate_invoice_for_on_hold_subscription( $amount_to_charge, $subscription ) {
// Check if the subscription is on-hold
if ($subscription->has_status('on-hold')) {
// Get the order related to the subscription
$order = wc_create_order(array(
'customer_id' => $subscription->get_customer_id()
));
// Add subscription items to the order
foreach ($subscription->get_items() as $item_id => $item) {
$product_id = $item->get_product_id();
$quantity = $item->get_quantity();
$order->add_product(wc_get_product($product_id), $quantity);
}
// Set billing information
$order->set_address($subscription->get_address('billing'), 'billing');
// Create invoice by changing the order status to pending
$order->update_status('pending');
// Send the invoice email to the customer
WC()->mailer()->get_emails()['WC_Email_Customer_Invoice']->trigger($order->get_id());
}
}
What I am expecting from the code:
- Checks if the subscription status is “on-hold”.
- Creating a new “pending” order cloned from the failed order.
- Send the invoice email notification to the customer.
How to allow invoices to continue generating even when the account is on hold?
5
Your current code can’t work as woocommerce_scheduled_subscription_payment
action hook, has only $subscription_id
as argument, but not $amount_to_charge
and $subscription
.
Also, you can try to change the related order status back to “pending”, instead of creating a new cloned order.
I see 2 ways to send WooCommerce invoice notification on failed payment when the subscription is “on-hold”:
1). From the unpaid subscription side:
add_action('woocommerce_subscription_payment_failed', 'generate_invoice_for_on_hold_subscription', 10, 2);
function generate_invoice_for_on_hold_subscription( $subscription, $new_status ) {
// Check if the subscription status is "on-hold"
if ( $new_status === 'on-hold' ) {
$orders_ids = (array) $subscription->get_related_order_ids(); // get related order IDs
// Loop through orders Ids for the current subscription
foreach( $orders_ids as $order_id ) {
// Get the WC_Order object
$order = wc_get_order( $order_id );
// Target the failed order
if ( $order->has_status( 'failed' ) ) {
// Tag the order with custom meta data
$order->update_meta_data('_invoice_sent', '1');
// Change back the order status to pending and save
$order->update_status( 'pending', esc_html__('Status change from failed to pending'), true );
// Send the invoice email to the customer
WC()->mailer()->get_emails()['WC_Email_Customer_Invoice']->trigger( $order->get_id() );
break; // Stop the loop
}
}
}
}
2). From the failed order side (if it doesn’t work):
add_action( 'woocommerce_order_status_failed', 'generate_invoice_for_on_hold_subscription', 10, 2 );
function generate_invoice_for_on_hold_subscription( $order_id, $order ){
// Get subscriptions from order ID
$subscriptions_ids = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => 'any' ) );
// Exit if there are no subscriptions linked to the current failed order
if ( ! $subscriptions_ids ) {
return;
}
// Get the WC_Subscription object
$subscription = new WC_Subscription( current($subscriptions_ids) );
// Check if the subscription status is "on-hold"
if ( $subscription->has_status( 'on-hold' ) && $order->get_meta('_invoice_sent', '1')) {
// Tag the order with custom meta data
$order->update_meta_data('_invoice_sent', '1');
// Change back the order status to pending and save
$order->update_status( 'pending', esc_html__('Status change from failed to pending'), true );
// Send the invoice email to the customer
WC()->mailer()->get_emails()['WC_Email_Customer_Invoice']->trigger( $order->get_id() );
}
}
Untested, it could work.