Phone Number as PDF Password
A customer asked about maybe using a customer’s phone number as the PDF password instead of their email or an admin-chosen string. Here is some example code which will get that done. Make sure to test it, and customize ad-lib to suit your purposes.
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 );
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. 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!
(advertisement)