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. This snippet might help you do that if you are struggling:
// Source - https://stackoverflow.com/a/79785167
// Posted by Joshua
// License - CC BY-SA 4.0
function force_phone_required_option() {
update_option( 'woocommerce_checkout_phone_field', 'required' );
}
add_action( 'init', 'force_phone_required_option');
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!
If you are familiar with the PHP coding language, scanning the block of code above will probably reveal a multitude of possibilities. This is partly because once you are given the $order object, you can grab all sorts of data from the database. This means you could set the file password not just to email or phone number, but to the buyer’s VAT or postal code or order number or other saved value – and also any combination of those pieces of data. In this sense, using the phone number is just here as an example.
If you have an idea for what you’d like your PDF passwords to be, and need some help with code, just reach out and we will try to help.
WaterWoo Code (for reference)
The following code will NOT work for PDF Ink. It is for WaterWoo, a different WordPress plugin. This code might be useful as reference during a migration.
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 );