Filter: WP-CRM System Email Notification Opportunity

The wp_crm_system_email_notifications_opportunity filter allows you to modify the various variables used to send an email notification for opportunities.

The filter is passed one argument as an array:

$send_email    = array(
    'send'        => true,
    'vars'        => $vars,
    'to'        => $to,
    'subject'    => $subject,
    'message'    => $message,
    'headers'    => $headers,
    'id'        => $ID,
    'post'        => $post,
);

The variables in the array can be used to modify a number of behaviors including how frequently a notification is sent, who it is sent to, the subject , message, and the headers sent along with the message.

In order for the email to be sent, $send_email[‘send’] must be true, which it is by default. If for some reason a condition is met where you do not want the email to be sent again, you can use the filter to set $send_email[‘send’] to false, which will prevent the email from sending.

When this filter is run, you can set custom metadata for the opportunity that can be used in the future, or use existing data to determine if an email should be sent, who it should be sent to, and more.

For example:

// Only send email if the opportunity won/lost status is 'won'
add_filter( 'wp_crm_system_email_notifications_opportunity', 'prevent_sending_until_opportunity_won' );
function prevent_sending_until_opportunity_won( $send_email ){
    $status = get_post_meta( $send_email['id'], '_wpcrm_opportunity-wonlost', true );
    if( 'won' != $status ){
        $send_email['send'] = false;
    }
    return $send_email;
}
// Only send email when the assigned user is changed
add_filter( 'wp_crm_system_email_notifications_opportunity', 'send_assigned_user' );
function send_assigned_user( $send_email ){
    // Assigned user in WP-CRM System
    $assigned = get_post_meta( $send_email['id'], '_wpcrm_opportunity-assigned', true );

    // Get previous assigned user
    $previous = get_post_meta( $send_email['id'], '_wpcrm_opportunity_email_notification_previously_assigned', true );

    if( $assigned == $previous ){
        // The assigned user is the same as the previously assigned user, so prevent sending.
        $send_email['send'] = false;
    } else {
        // Set the currently assigned user as a custom post meta value so we can check for it again in the future.
        update_post_meta( $send_email['id'], '_wpcrm_opportunity_email_notification_previously_assigned', $assigned );
    }
    return $send_email;
}