1

I need to get the subscription key of a particular subscription to pass into a custom function. I have referenced the documentation where it shows how to get the key, but I have failed to integrate this into my code. So what my code does is, when a renewal is triggered, I hook into processed_subscription_payment with my function. So this code is run only when a subscription renewal is payed. Code is below.

Documentation is here: https://docs.woothemes.com/document/subscriptions/develop/functions/management-functions/

Code here (which resides in functions.php):

add_action( 'processed_subscription_payment', 'callupdater' );

function callupdater()
{
    //need to get the subscription key here to pass to updatedays()
    $key = .....
    updatedays(key);
}

function updatedays($subscription_key)
{
    //do some tasks with the key
}

Any help is very much appreciated. I am very new to PHP so excuse my ignorance.

2 Answers 2

2

I figured out the answer, so I thought I'd post it. My code looks like this now and it works:

add_action( 'processed_subscription_payment', 'updatedays', 10, 2 );

function updatedays($user_id, $subscription_key)
{  
    //do what I need to the sub key
}

There really needs to be more examples on the Woothemes documentation though..

1
  • 1
    Agreed. They are in need of some better documentation.
    – Cam
    Apr 14, 2016 at 11:56
1

As long as you can get Order Id, you can can use this code.

global $woocommerce;
$order_id=12345;//PUT YOUR ORDER ID HERE
$order = new WC_Order( $order_id );
foreach ( WC_Subscriptions_Order::get_recurring_items( $order ) as $order_item ) {
    $subscription_key = WC_Subscriptions_Manager::get_subscription_key( $order->id, WC_Subscriptions_Order::get_items_product_id( $order_item ) );
}
echo $subscription_key;

Not the answer you're looking for? Browse other questions tagged or ask your own question.