Creating your Own Watermarking Shortcodes for WooCommerce

You can add your own shortcodes to WaterWoo PDF Premium for on-the-fly customized watermarks. In this simple example we are going to create a shortcode to reflect a customer’s order total (price) in the watermark, when using a shortcode: [TOTAL_PRICE]

function fetch_woo_order_data_as_shortcode( $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 );

    // now let's get the order data from the order object
    $order_data = $order->get_data();
    $order_total = $order_data['total']; // BINGO!
    // there is a ton of other data you can get from the WC order object data, check out
    // https://stackoverflow.com/questions/39401393/how-to-get-woocommerce-order-details
    // and substitute in what you need into the code that follows below

    // if there isn't a total for some reason, we give up
    if ( ! $order_total || $order_total == '' ) {
        return $input;
    }
 
    // you can manipulate the format of the total here if you wish.
 
    // when we find [TOTAL_PRICE] in a watermark, replace it with the order total
    $input = preg_replace( '/\[TOTAL_PRICE\]/', $order_total, $input );
 
    return $input;
}
add_filter( 'wwpdf_filter_overlay', 'fetch_woo_order_data_as_shortcode', 10, 3 ); // for filtering the overlay watermark
add_filter( 'wwpdf_filter_footer', 'fetch_woo_order_data_as_shortcode', 10, 3 ); // for filtering the footer watermark

Now when you use [TOTAL_PRICE] in your watermark, the PDF will show the total price of the WooCommerce order the PDF was purchased in. Perhaps this way you could print out the price on a gift certificate. More on how to create gift certificates using WaterWoo and EDDiMark can be found here.

The comments in the code above give clues about how to use code in similar ways to get other WooCommerce order values. If you’re unfamiliar with PHP and Wordpress codex, we recommend hiring a developer to help sort you out. Especially with this example code, it should make their job easy… and you’ll have a customized watermark!