Woocommerce action hook to execute function on subscription renewal

Viewed 9022

I want to know if there is a action hook that can check if the subscription is successfully renewed in woocommerce ? BTW I am using woocommerce subscription plugin. I have created a functionality that records the date of the subscription order and add it to a CSV file, the function is working perfectly for the first purchase I mean when the user purchase a subscription it is recorded successfully in the CSV because I am firing up the function on woocommerce_thankyou action hook, The only issue I am facing is that I can't seem to find a hook which can execute this function on successful subscription renewal. I tried to use woocommerce_subscription_renewal_payment_complete action hook but it didn't worked below is the function that I have created.

/**
 * Add subscriptions to csv.
 */

add_action( 'woocommerce_subscription_renewal_payment_complete', 'add_subs_to_csv' );
add_action( 'woocommerce_thankyou', 'add_subs_to_csv' );
function add_subs_to_csv( $order_id ) {
    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    foreach ( $items as $key => $value ) {
        $meta_values = $value->get_data();
        foreach ( $meta_values as $meta_key => $meta_value ) {
            if ( $meta_key == 'product_id' && $meta_value == 875 ) {
                $paid_date      = explode( " ", get_post_meta( $order_id, '_paid_date', true ) );
                $subs_paid_date = date( 'd F, Y', strtotime( $paid_date[0] ) );
                wc_add_order_item_meta( $key, 'Delivery Date', $subs_paid_date );
            }
        }

    }
}
2 Answers

I had a issue at Subscription renewal and I got it fixed with below code:

/*
 * FIXED : Membership got PAUSED everytime at automatic subscription renewal
 */

function change_membership_status_active( $subscription , $order ) {
    global $wpdb;
    if( 'completed' === $order->get_status() ) {        
        $membership = $wpdb->get_row( "SELECT * FROM wp_postmeta WHERE meta_key = '_subscription_id' AND meta_value =  $subscription->ID" );
        $mem_id = $membership->post_id;
        $status = 'wcm-active';
        $update_args = array( 'ID' => $mem_id, 'post_status' => $status );
        wp_update_post($update_args);
    }
}
add_action( 'woocommerce_subscription_renewal_payment_complete', 'change_membership_status_active', 10, 2 );
Related