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.

Another Example

In the following example I show how to use the $product_id parameter to wheedle out product information for a shortcode which will show the product SKU. Our shortcode will be [ITEM_SKU]. Check it out:

function fetch_woo_product_data_for_shortcode( $input, $order_id, $product_id ) {
 
    // first we need to get the product object from the product id
    // you can get a lot of info from the product object ( $order in this case)
    $product = wc_get_product( $product_id );

    if ( ! is_a( $product, 'WC_Product' ) ) {
        return $input;
    }

    // there is a ton of other data you can get from the WC product object, check out
    // https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
    // and substitute in what you need into the code that follows below
    $sku = $product->get_sku();

    // if there isn't a SKU for some reason, we give up
    if ( empty( $sku ) ) {
        return $input;
    }
 
    // when we find [ITEM_SKU] in a watermark, replace it with the product SKU
    $input = preg_replace( '/[ITEM_SKU]/', $sku, $input );
 
    return $input;
}
add_filter( 'wwpdf_filter_overlay', 'fetch_woo_product_data_for_shortcode', 10, 3 ); // for filtering the overlay watermark
add_filter( 'wwpdf_filter_footer', 'fetch_woo_product_data_for_shortcode', 10, 3 ); // for filtering the footer watermark

The comments in the code above give clues about how to use code in similar ways to get other WooCommerce order and product values. There is a lot of WooCommerce data that can be obtained from the parameters in these filter hooks. 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!


Buy WaterWoo PDF Premium Here!


Purchase includes one year of updates and support
After 1 year, renew manually when ready, with 35% discount

30-day refund – for any reason!


(advertisement)