Home / Admin / WooCommerce Wholesale Lead Capture Re-send Email Button in User Profile
Duplicate Snippet

Embed Snippet on Your Site

WooCommerce Wholesale Lead Capture Re-send Email Button in User Profile

This snippet will add a resend email button under User profile.

Code Preview
php
<?php
/***********************************************************************************
 * WooCommerce Wholesale Lead Capture Re-send Email Button in User Profile
 * *********************************************************************************/
function wwlc_add_resend_email_button() {
    if ( ! is_plugin_active( 'woocommerce-wholesale-lead-capture/woocommerce-wholesale-lead-capture.bootstrap.php' ) ) {
        return;
    }
?>
    <table class="form-table resend-wholesale-lead-email">
        <tr>
            <th><label for="user_role">Re-send wholesale lead email</label></th>
            <td>
                <select class="email-type">
                    <option value="wholesale_application_received">Wholesale application received</option>
                    <option value="wholesale_account_approved">Wholesale account approved</option>
                    <option value="wholesale_account_rejected">Wholesale account rejected </option>
                </select>
                <button class="button button-default send-email-trigger" />Send email</button>
            </td>
        </tr>
    </table>
    <script>
        jQuery( document ).ready( function ( $ ) {
            $('.resend-wholesale-lead-email .send-email-trigger').click(function(e){
                e.preventDefault();
                var $parent       = $(this).closest('.resend-wholesale-lead-email');
                var emailType     = $parent.find('select.email-type').val();
                var emailTypeName = $parent.find('select.email-type option:selected').html();
                var userId        = $parent.closest('form').find('input#user_id').val();
                $.ajax({
                    url: ajaxurl,
                    type: "GET",
                    data: { action: "resend_wholesale_lead_email", userId: userId, emailType: emailType },
                    dataType: "json"
                }).done(function(response) {
                    toastr.success("", emailTypeName + ' successfully sent to ' + response.user_data.data.user_email, {
                        closeButton: true,
                        showDuration: 10000,
                        positionClass: "toast-bottom-right"
                    });
                });
            });
        } );
    </script>
<?php
}
add_action( 'show_user_profile', 'wwlc_add_resend_email_button', 99 );
add_action( 'edit_user_profile', 'wwlc_add_resend_email_button', 99 );
function wwlc_resend_wholesale_lead_email() {
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
        $user_id    = $_GET['userId'];
        $email_type = $_GET['emailType'];
    }
    $wc_emails = WC()->mailer()->get_emails();
    $user_data = get_userdata( $user_id );
    if ( $user_data ) {
        switch ( $email_type ) {
            case 'wholesale_application_received':
                $wc_emails['WWLC_Email_Wholesale_Application_Received']->trigger( $user_data, array(), '' );
                break;
            case 'wholesale_account_approved':
                $wc_emails['WWLC_Email_Wholesale_Account_Approved']->trigger( $user_data );
                break;
            case 'wholesale_account_rejected':
                $wc_emails['WWLC_Email_Wholesale_Account_Rejected']->trigger( $user_data );
                break;
            default:
                break;
        }
    }
    
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
        header( 'Content-Type: application/json' ); // specify we return json.
        echo wp_json_encode(
            array(
                'status'    => 'success',
                'user_data' => $user_data,
            )
        );
        die();
    } else {
        return true;
    }
}
add_action( 'wp_ajax_resend_wholesale_lead_email', 'wwlc_resend_wholesale_lead_email', 10 );

Comments

Add a Comment