Still send WooCommerce customer invoice notification on subscription failed payment

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật