是否有根据订阅状态触发操作的 Woocommerce 订阅功能?
Is there a Woocommerce Subscription function that triggers actions based on subscription status?
我使用一个名为 Mailster 的 Wordpress 电子邮件插件。我想设置它,以便如果用户取消他们的 Woocommerce 订阅,或者将其暂停或暂停,他们将被移至不同的电子邮件列表。
我有一个来自 Mailster 本身的代码来执行列表更改。代码如下。
add_action( 'my_custom_action', function( ) {
if( $subscriber = mailster( 'subscribers' )->get_by_wpid() ){
$list_id_from = 1;
$list_id_to = 2;
mailster( 'subscribers' )->unassign_lists( $subscriber->ID, $list_id_from );
mailster( 'subscribers' )->assign_lists( $subscriber->ID, $list_id_to );
}
});
我需要弄清楚的是如何将其与 Woocommerce 订阅集成,以便在订阅状态更改(取消、暂停、活动、暂停)时触发上面的代码。
我真的不知道该如何处理。我知道有几个钩子
"woocommerce_order_status_changed" and "woocommerce_subscription_status_updated"
但我不知道如何将它们合并到功能代码中。提前致谢。
更新
我可以确认,我对代码稍作修改后,它可以正常工作。以下是我使用过的完整代码。
function moved_to_a_different_email_list_when_subscription_status_updated( $subscription, $new_status, $old_status ){
if( $subscriber = mailster( 'subscribers' )->get_by_wpid() ){
$list_id_from = 5;
$list_id_to = 6;
if( $new_status == 'cancelled' || $new_status == 'pending-cancel' ){
mailster( 'subscribers' )->unassign_lists( $subscriber->ID, $list_id_from );
mailster( 'subscribers' )->assign_lists( $subscriber->ID, $list_id_to );
}
}
}
add_action('woocommerce_subscription_status_updated', 'moved_to_a_different_email_list_when_subscription_status_updated', 10, 3 );
检查下面的代码。您可以使用 add_action
来使用 woocommerce_order_status_changed
和 woocommerce_subscription_status_updated
动作挂钩。
function moved_to_a_different_email_list_when_order_status_changed( $order_id, $previous_status, $next_status, $WC_Order ) {
if( $subscriber = mailster( 'subscribers' )->get_by_wpid() ){
$list_id_from = 1;
$list_id_to = 2;
if( $next_status == 'cancelled' ){
mailster( 'subscribers' )->unassign_lists( $subscriber->ID, $list_id_from );
}else{
mailster( 'subscribers' )->assign_lists( $subscriber->ID, $list_id_to );
}
}
};
add_action( 'woocommerce_order_status_changed', 'moved_to_a_different_email_list_when_order_status_changed', 10, 4 );
function moved_to_a_different_email_list_when_subscription_status_updated( $subscription, $new_status, $old_status ){
if( $subscriber = mailster( 'subscribers' )->get_by_wpid() ){
$list_id_from = 1;
$list_id_to = 2;
if( $new_status == 'cancelled' ){
mailster( 'subscribers' )->unassign_lists( $subscriber->ID, $list_id_from );
}else{
mailster( 'subscribers' )->assign_lists( $subscriber->ID, $list_id_to );
}
}
}
add_action('woocommerce_subscription_status_updated', 'moved_to_a_different_email_list_when_subscription_status_updated', 10, 3 );
我使用一个名为 Mailster 的 Wordpress 电子邮件插件。我想设置它,以便如果用户取消他们的 Woocommerce 订阅,或者将其暂停或暂停,他们将被移至不同的电子邮件列表。
我有一个来自 Mailster 本身的代码来执行列表更改。代码如下。
add_action( 'my_custom_action', function( ) {
if( $subscriber = mailster( 'subscribers' )->get_by_wpid() ){
$list_id_from = 1;
$list_id_to = 2;
mailster( 'subscribers' )->unassign_lists( $subscriber->ID, $list_id_from );
mailster( 'subscribers' )->assign_lists( $subscriber->ID, $list_id_to );
}
});
我需要弄清楚的是如何将其与 Woocommerce 订阅集成,以便在订阅状态更改(取消、暂停、活动、暂停)时触发上面的代码。
我真的不知道该如何处理。我知道有几个钩子
"woocommerce_order_status_changed" and "woocommerce_subscription_status_updated"
但我不知道如何将它们合并到功能代码中。提前致谢。
更新
我可以确认,我对代码稍作修改后,它可以正常工作。以下是我使用过的完整代码。
function moved_to_a_different_email_list_when_subscription_status_updated( $subscription, $new_status, $old_status ){
if( $subscriber = mailster( 'subscribers' )->get_by_wpid() ){
$list_id_from = 5;
$list_id_to = 6;
if( $new_status == 'cancelled' || $new_status == 'pending-cancel' ){
mailster( 'subscribers' )->unassign_lists( $subscriber->ID, $list_id_from );
mailster( 'subscribers' )->assign_lists( $subscriber->ID, $list_id_to );
}
}
}
add_action('woocommerce_subscription_status_updated', 'moved_to_a_different_email_list_when_subscription_status_updated', 10, 3 );
检查下面的代码。您可以使用 add_action
来使用 woocommerce_order_status_changed
和 woocommerce_subscription_status_updated
动作挂钩。
function moved_to_a_different_email_list_when_order_status_changed( $order_id, $previous_status, $next_status, $WC_Order ) {
if( $subscriber = mailster( 'subscribers' )->get_by_wpid() ){
$list_id_from = 1;
$list_id_to = 2;
if( $next_status == 'cancelled' ){
mailster( 'subscribers' )->unassign_lists( $subscriber->ID, $list_id_from );
}else{
mailster( 'subscribers' )->assign_lists( $subscriber->ID, $list_id_to );
}
}
};
add_action( 'woocommerce_order_status_changed', 'moved_to_a_different_email_list_when_order_status_changed', 10, 4 );
function moved_to_a_different_email_list_when_subscription_status_updated( $subscription, $new_status, $old_status ){
if( $subscriber = mailster( 'subscribers' )->get_by_wpid() ){
$list_id_from = 1;
$list_id_to = 2;
if( $new_status == 'cancelled' ){
mailster( 'subscribers' )->unassign_lists( $subscriber->ID, $list_id_from );
}else{
mailster( 'subscribers' )->assign_lists( $subscriber->ID, $list_id_to );
}
}
}
add_action('woocommerce_subscription_status_updated', 'moved_to_a_different_email_list_when_subscription_status_updated', 10, 3 );