Search code examples
phpwordpresswoocommercecheckoutemail-notifications

How can I exclude WooCommerce checkout from sending user profile change emails?


We have a shop that is connected to a item management software. It exports its data via csv (WP All Export).

Now we have a function that fills a users description field with a text when profile data is changed.

Only users with this text get exported in the csv and once exported, the text ist gone and it isn't exported until the next change. It also fires an email when data is changed.

The problem is: This email is also sent every time the user orders something, even when his profile data stays the same. This has to be avoided.

Is there a way to exclude the checkout from firing this email?

The mentioned function is below:

function my_profile_update( $user_id ) {    
    
    if (!defined( 'WP_IMPORTING' ) ) { 
    
        $user_geaendert = get_user_meta( $user_id, 'user_geaendert', false );

        if (!strpos($user_geaendert, 'Export')) {

            update_user_meta( $user_id, 'description', 'seit letztem Export geändert ' . date("Y-m-d H:i:s"));          
            
            $to = '[email protected]';
            $subject = "Benutzerdaten geändert";
            $message =  'Benutzer mit der ID '.$user_id.' hat seine Daten geändert. domain.com/wp-admin/user-edit.php?user_id='.$user_id;       
            
            wp_mail($to, $subject, $message);
            
        }
    }   else {
        
            update_user_meta( $user_id, 'description', '');
    }
}
add_action( 'profile_update', 'my_profile_update' );
//add_action( 'edit_user_profile_update', 'my_profile_update' );
//add_action( 'personal_options_update', 'my_profile_update' );


function user_role_update( $user_id, $new_role ) {
        
    
        $site_url = get_bloginfo('wpurl');  
        $user_info = get_userdata( $user_id );
    
    
        $user_meta  =   get_userdata($user_id); 
        $user_roles =   $user_meta->roles; 
        
        if (in_array("customer", $user_roles)){
            $user_roles = 'customer';
        }
    
    
        if (get_user_meta( $user_id, 'anu_new_user_pass' , true ) != '' && $user_roles == 'customer') {
            
            $to = $user_info->user_email;
            $subject = "Freischaltung bei XXX";
            $message = "Sehr geehrte/r Nutzer/in,

    Ihr Benutzerkonto auf xxx.de wurde nun von uns freigeschaltet.

    Bitte loggen Sie sich mit Ihren Zugangsdaten ein. Ihre Kundennummer finden Sie in Ihrem Benutzerprofil.<br><br>
    Login: domain.com/mein-konto/ <br>
    Benutzername: ".get_user_meta( $user_id, 'nickname' , true ) . "<br>
    Passwort: ".get_user_meta( $user_id, 'anu_new_user_pass' , true );
            
            
            wp_mail($to, $subject, $message);
            
        }
        
}
add_action( 'set_user_role', 'user_role_update', 10, 2);

Solution

    • Added is_checkout() - Returns true when viewing the checkout page.
    • Code has been cleaned up by adding comments with explanations
    function my_profile_update( $user_id ) {    
        // NOT defined
        if ( ! defined( 'WP_IMPORTING' ) ) { 
            // Get user meta
            $user_geaendert = get_user_meta( $user_id, 'user_geaendert', false );
    
            // Find the position of the first occurrence of a substring in a string, Returns FALSE if the needle was not found.
            if ( ! strpos( $user_geaendert, 'Export' ) ) {
                // Update user meta
                update_user_meta( $user_id, 'description', 'seit letztem Export geändert ' . date( 'Y-m-d H:i:s' ) ); 
    
                // NOT on the checkout page
                if ( ! is_checkout() ) {       
                    // Mail parameters
                    $to = '[email protected]';
                    $subject = "Benutzerdaten geändert";
                    $message =  'Benutzer mit der ID ' . $user_id . ' hat seine Daten geändert. domain.com/wp-admin/user-edit.php?user_id=' . $user_id;
                    
                    // Sends an email, similar to PHP’s mail function.
                    wp_mail( $to, $subject, $message );
                }               
            }
        } else {
            // Update user meta
            update_user_meta( $user_id, 'description', '');
        }
    }
    add_action( 'profile_update', 'my_profile_update' );