Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce action hook to execute function on subscription renewal

Tags:

wordpress

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 );
            }
        }

    }
}
like image 645
Omer Avatar asked Jul 19 '17 11:07

Omer


2 Answers

Could the wcs_renewal_order_created hook be what you're looking for? The docs say:

WooCommerce Subscriptions stores all details of each subscription renewal in a standard WooCommerce order, only with a special meta flag linking it to a subscription.

These orders are always created through the wcs_create_renewal_order() function, regardless of whether they are created for a scheduled renewal event, manually via the WooCommerce > Edit Subscription administration screen, or via the Subscriptions endpoints for the WooCommerce REST API. Because of this, it’s possible to add, remove or update the value of anything on that renewal order using this filter.

For example, this can be used to add a discount to specific renewal orders, like the 12th order each year. It could also be used to add one-time fee for a certain renewal order, like a special annual extra fee on a monthly subscription.

So the above hook should trigger after payment, you'd probably just need to check if it was completed status which you could also do in your current hooks:

/**
 * After WooCommerce Subscriptions Creates Renewal Order
 *
 * @param WC_Order Object $order
 * @param Integer|WC_Subscription Object $subscription
 *
 * @return void
 */
function add_subs_to_csv( $order, $subscription ) {

    if( 'completed' === $order->get_status() ) {
        $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 );
                }
            }

        }
    }

}
add_action( 'wcs_renewal_order_created', 'add_subs_to_csv', 10, 2 );
like image 89
Howdy_McGee Avatar answered Oct 22 '22 16:10

Howdy_McGee


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 );
like image 28
Bhautik Nada Avatar answered Oct 22 '22 18:10

Bhautik Nada