Search code examples
wordpresswoocommercehook-woocommerceemail-clientwoocommerce-subscriptions

Is there a Woocommerce Subscription function that triggers actions based on subscription status?


I use a Wordpress email plugin called Mailster. I would like to set it up so that if a user cancels their Woocommerce Subscription, or puts it on pause or suspends it, they are moved to a different email list.

I have a code from Mailster itself that executes the list change. The code is below.

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

What I need to figure out is how to integrate this with Woocommerce Subscriptions so that when the subscription status is changed (cancelled, paused, active, suspended), the code above is triggered.

I'm really not sure how to approach it. I know there's a couple of hooks

"woocommerce_order_status_changed" and "woocommerce_subscription_status_updated"

But I have no idea how to incorporate them into a functional code. Thanks in advance.

UPDATE

I can confirm that with a little modification I made to the code, it works. Below is the full code that I've used.

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

Solution

  • Check the below code. you can use add_action to use woocommerce_order_status_changed and woocommerce_subscription_status_updated action hooks.

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