0

Based on the information in woocommerce documentation: https://docs.woocommerce.com/document/subscriptions/develop/action-reference/
The action: woocommerce_subscription_status_changed,
Should be triggered also when the subscription upgraded or downgraded - switched,
But it only works on the following situations:
active, on-hold, cancelled
Here is my sample code:

add_action('woocommerce_subscription_status_changed', 'test', 10, 3);
function test( $subscription_id, $old_status, $new_status ) {
    global $woocommerce;
    $file_content = $subscription_id.' '.$old_status.' '.$new_status;
    $filename = '/tmp/test_file.txt';
    file_put_contents($filename, $file_content);
}


The code above works, but not when the subscription switched, my question is why ?

Shlomi
  • 337
  • 1
  • 6
  • 19

2 Answers2

0

You should use woocommerce_subscriptions_switch_completed action istead for this purpose

Rickard T
  • 1
  • 1
0

The hook you are using

     add_action('woocommerce_subscription_status_changed', 'test', 10, 3);  <== 2.0 or earlier

has become obsolete and only works in woocommerce subscription 2.0 or below. 2.0+ and above uses the following hook.

 add_action( 'woocommerce_subscription_status_updated', 'test', 1, 3); <=== after 2.0

 function test( $subscription_id, $old_status, $new_status ) {
     $file_content = $subscription_id.' '.$old_status.' '.$new_status;
     $filename = '/tmp/test_file.txt';
     file_put_contents($filename, $file_content);
   }

You can remove the global $woocommerce, as it is not longer necessary.

Debbie Kurth
  • 403
  • 3
  • 16
  • 1
    no no, from doc https://docs.woocommerce.com/document/subscriptions/develop/action-reference/, that method carries an instance of WC_Subscription object. so it should not be $subscription_id, but $subscription object. I have read the documentation more than once – Karue Benson Karue Feb 25 '21 at 12:56
  • 1
    The parameters seems wrong here as per the doc It should be test( $subscription_obj, $new_status , $old_status) – tousif Dec 14 '21 at 13:02
  • They work..actively using the code – Debbie Kurth Dec 14 '21 at 19:10