Sent an Email to administrator on new subscription

Is it possible to sent a notification e-mail to the administrator when a user activates a new subscription?

I would like to give users instant acces to content on our website. But I also want to process their subscription manually in another program. so I want to be notified when there is a new subscription at a particular level.

  • Hoang Ngo
    • Code Slayer

    Hi there @Emiel,

    I hope you are well today

    Here is an example of code, which will send the admin an email when new subscription arrived.

    Please copy this code, save as a php file example "notice_admin_when_new_subscription.php", and place that file to mu-plugins folder.

    <?php
    /**
     * Author: Hoang Ngo (incsub)
     */
    
    add_action( 'membership_payment_processed', 'send_email_when_new_subscription', 12, 5 );
    function send_email_when_new_subscription( $user_id, $sub_id, $amount, $currency, $trans_id ) {
    	$user_info = get_userdata( $user_id );
    
    	$name = trim( $user_info->first_name . ' ' . $user_info->last_name );
    	if ( empty( $name ) ) {
    		$name = $user_info->user_login;
    	}
    
    	global $wpdb;
    	$sql          = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'm_membership_levels WHERE id=%d', array( $sub_id ) );
    	$subscription = $wpdb->get_row( $sql );
    
    	$message = 'New subscription!' . PHP_EOL;
    	$message .= 'Customer name: ' . $name . PHP_EOL;
    	$message .= 'Subscription name: ' . $subscription->level_title . PHP_EOL;
    	$message .= 'Amount: ' . $currency . $amount;
    	wp_mail( get_option( 'admin_email' ), 'New Subscription', $message );
    }

    If you have any issues please don't hesitate to let us know so we can assist.

    Best Regards
    Hoang

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.