Brazilian Shops can Watermark CPF
You can add your own shortcodes to WaterWoo PDF Premium for on-the-fly customized watermarks.
One request from a Brazilian customer was for a shortcode for the Cadastro de Pessoas Físicas (CPF). This is a taxpayer identification number all citizens of Brazil must use to shop online within Brasil. We created the shortcode [BILLING_CPF] to import any customer’s CPF number into their watermarked (personalized) PDF.
Here is the code to add to a (child) theme functions.php file:
function fetch_custom_checkout_field( $input, $order_id, $product_id ) {
// first we need to get the order object from the order id
// you can get a lot of info from the order object ( $order in this case)
$order = wc_get_order( $order_id );
// The CPF plugin stores CPFs in WooCommerce order meta, making it easy to fetch
// you might want to do testing to make sure the following line, if altered
// actually fetches what *you* need from the DB:
$billing_cpf = $order->get_meta( '_billing_cpf' );
// if there isn't a CPF, we give up
if ( ! $billing_cpf || $billing_cpf === '' ) {
return $input;
}
// remove . - / from CPF, keep it numbers only
$billing_cpf = str_replace( [ '.', '-', '/' ], '', $billing_cpf );
// when we find [BILLING_CPF] in a watermark, replace it with the order CPF
$input = preg_replace( '/[BILLING_CPF]/', $billing_cpf, $input );
return $input;
}
add_filter( 'wwpdf_filter_overlay', 'fetch_custom_checkout_field', 10, 3 );
add_filter( 'wwpdf_filter_footer', 'fetch_custom_checkout_field', 10, 3 );
This is simple PHP code. It can be edited ad lib to accomplish your particular watermarking goals. Here’s a nice list of all the order details you can fetch using the order object.
Please note this shortcode will not work with the PDF testing feature, because it doesn’t run through WooCommerce – there is no purchase. You will need to set up a private dummy product (price: 0.00) and live test with a purchase to confirm its function.
In order to collect CPF data from customers, our customer was using the Woocommerce Extra Checkout Fields for Brazil plugin.*
*(This CPF plugin probably does what it says, and this link is not an endorsement.)
CPF for the WP TCPDF Bridge Watermarking Plugin
WP TCPDF Bridge has similar filter hooks for the two watermark placements; however, different information is passed to the hook, and that information is unpredictable and dynamic depending on how the TCPDF Bridge shortcodes are used. That said, here is some example code for what could be done:
function fetch_custom_cpf_checkout_field( $input, $post_data ) {
// If we KNOW we have a logged in user, get current user:
if ( is_user_logged_in() ) {
$current_user_id = get_current_user_id();
// Get the WC_Customer instance Object for our current user
$customer = new WC_Customer( $current_user_id );
if ( ! $customer ) {
return $input;
}
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
} else {
// Here we could go through $post_data, which is the $_POST data
// Some helpful info here: https://stackoverflow.com/questions/22843504/how-can-i-get-customer-details-from-an-order-in-woocommerce
// I will leave this up to your team. For now:
return $input;
}
// The CPF plugin stores CPFs in WooCommerce order meta, making it easy to fetch
// you might want to do testing to make sure the following line, if altered
// actually fetches what *you* need from the DB:
$billing_cpf = $last_order->get_meta( '_billing_cpf' );
// If there isn't a CPF, we give up
if ( ! $billing_cpf || $billing_cpf === '' ) {
return $input;
}
// remove . - / from CPF, keep it numbers only
$billing_cpf = str_replace( [ '.', '-', '/' ], '', $billing_cpf );
// when we find [BILLING_CPF] in a watermark, replace it with the order CPF
return preg_replace( '/[BILLING_CPF]/', $billing_cpf, $input );
}
add_filter( 'wptcpdfb_filter_overlay', 'fetch_custom_cpf_checkout_field', 10, 2 );
add_filter( 'wptcpdfb_filter_footer', 'fetch_custom_cpf_checkout_field', 10, 2 );
(advertisement)