Creating your Own Watermarking Shortcodes for WooCommerce

Cartoon image of someone with curly hair holding binoculars, nothing in particular reflected in their lenses.

There are quite a few built in shortcodes, but sometimes we just need something… special. You can add your own shortcodes to PDF Ink for on-the-fly customized watermarks on PDF pages.

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 custom shortcode: [TOTAL_PRICE]

function total_price_special_shortcode( $content, $key, $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 $content;
    }

    // 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
    $content = preg_replace( '/[TOTAL_PRICE]/', $order_total, $content );

    return $content;
}
add_filter( 'pdfink_filter_placement_content', 'total_price_special_shortcode', 10, 4 );

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 PDF Ink 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 item_sku_special_shortcode( $content, $key, $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 $content;
    }

    // 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 $content;
    }

    // when we find [ITEM_SKU] in a watermark, replace it with the product SKU
    $content = preg_replace( '/[ITEM_SKU]/', $sku, $content );

    return $content;
}
add_filter( 'pdfink_filter_placement_content', 'item_sku_special_shortcode', 10, 4 );

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!

To top