woocommerce订阅切换操作不起作用

时间:2017-04-03 17:35:31

标签: php wordpress woocommerce orders woocommerce-subscriptions

根据woocommerce文档中的信息: https://docs.woocommerce.com/document/subscriptions/develop/action-reference/
操作:woocommerce_subscription_status_changed
当订阅升级或降级时也应触发 - switched
但它仅适用于以下情况:
active, on-hold, cancelled
这是我的示例代码:

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


上面的代码有效,但是当订阅切换时,我的问题是为什么?

2 个答案:

答案 0 :(得分:0)

你应该为此目的使用woocommerce_subscriptions_switch_completed动作

答案 1 :(得分:0)

您正在使用的钩子

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

已过时,仅在woocommerce订阅2.0或更低版本中可用。 2.0及更高版本使用以下钩子。

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

您可以删除全局$ woocommerce,因为它不再需要。