Phone Number as PDF Password

Cartoon graphic of a few words: "phone number password"

PDF Ink comes with the built-in ability to use a customer’s email as PDF password, but what if we want to use their phone number or an admin-chosen string instead? Here is some example code which will get that done. Make sure to test it, and customize ad-lib to suit your purposes.

function use_phone_as_password( $settings, $post_id, $variation_id, $source ) {

    $phone = '';

    // Start by maybe getting phone number from order ID
    $order = wc_get_order( $order_id );
    $order_data = $order->get_data();
    $phone = $order_data['billing']['phone'];

    if ( ! isset( $phone ) || $phone == '' ) { 
        // No phone number yet, try seeing if user is logged in and has a saved phone number:
        if ( is_user_logged_in() ) {
            $user = wp_get_current_user();
            $phone = get_user_meta( $user->ID, 'billing_phone', true ) ? get_user_meta( $user->ID, 'billing_phone', true ) : '';
        }
    }

    // You might want to manipulate the phone number to remove +- ( first, like this:
    // $phone = preg_replace( '/D+/', '', $phone ); // un-comment to clean up and only use the digits

    if ( ! isset( $phone ) || $phone == '' ) {
        return $settings;
    }
    $settings['security']['pwd'] = $phone;
    // Return phone number as the password
    return $settings; 
}
add_filter( 'pdfink_filter_settings', 'use_phone_as_password', 10, 4 );

Of course if you want to set the password as the phone number, you will need to make the phone number field mandatory in your checkout. Phone as password is not a default setting in PDF Ink because WooCommerce carts do not require a phone number by default (it is optional). So make sure to make that field REQUIRED if you are going to set PDF passwords by it. You will also want to find a way to clearly notify your customers what their password will be, or else you will be answering a lot of emails!


WaterWoo code for reference (deprecated, for use in migrations):

function waterwoo_use_phone_as_password( $password, $order_id ) {

    $phone = '';

    // start by maybe getting phone number from order ID
    $order = wc_get_order( $order_id );
    $order_data = $order->get_data();
    $phone = $order_data['billing']['phone'];

    if ( ! isset( $phone ) || $phone == '' ) { 
	// no phone number yet, try seeing if user is logged in and has a saved phone number:
        if ( is_user_logged_in() ) {
            $user = wp_get_current_user();
            $phone = get_user_meta( $user->ID, 'billing_phone', true ) ? get_user_meta( $user->ID, 'billing_phone', true ) : '';
        }
    }
   
    // you might want to manipulate the phone number to remove +- ( first, like this:
    // $phone = preg_replace( '/D+/', '', $phone ); // uncomment to clean up and only use the digits
    
    if ( ! isset( $phone ) || $phone == '' ) {
        return $password;
    }
    return $phone; // Returns phone number as the password

}
add_filter( 'wwpdf_set_password', 'waterwoo_use_phone_as_password', 10, 2 );

Note: the code directly above will NOT work for PDF Ink. It is for WaterWoo, a different WordPress plugin.

To top