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( array( '.', '-', '/' ), '', $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.)