If you would like to create a custom magic tag to display dynamic output, that can usually be done using filter hooks included in PDF Ink. The filter hook to use is ‘pdfink_filter_placement_content’ and the parameters it passes are $content, $key, $order_id and $product_id. $key refers to the placement: 0 is the first placement, 1 is the second, and if you have more, they’d be numbered 2, 3, 4 etc.
PSST! The ‘pdfink_filter_placement_content‘ hook also works for Easy Digital Downloads and Download Monitor, with nearly identical parameters. Keep reading!
Unless you are adding static content (such as the hour), or global data (such as the $_SERVER[‘REMOTE_ADDR’]), it’s necessary that you have collected and have reliable access to data from the your database. E-commerce has collected customer and other order data for you during checkout, storing it in the WordPress database. An order ID and product ID should allow you to fetch all sorts of related data.
An abstract example looks like this:
function fetch_woo_order_data_for_magic( $content, $key, $order_id, $product_id ) {
// first could 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 );
if ( ! $order ! is_a( $order, 'WC_Order' ) ) {
error_log( 'No WC_Order object.' );
return $content; // make sure to return $input if something goes wrong
}
// Examples of getting some order data from the order object, lots of data can be accessed
// More info: https://stackoverflow.com/a/44708344
$order_data = $order->get_data();
$order_total = $order_data['total']; // getting the order total data
// More info on grabbing product data: https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
// ...find and sanitize the data you seek
$the_data_we_seek = 'woohoo I found it!'; // replace with actual value
// if/when we find [MY_CUSTOM_MAGIC_TAG] in the content, replace it with the $the_data_you_seek
$content = preg_replace( '/\[MY_CUSTOM_MAGIC_TAG\]/', $the_data_we_seek, $content );
return $content;
}
add_filter( 'pdfink_filter_placement_content', 'fetch_woo_order_data_for_magic', 10, 4 );
Here we chose to make an arbitrary magic tag called [MY_CUSTOM_MAGIC_TAG]. When we find [MY_CUSTOM_MAGIC_TAG] in the PDF Ink content, we replace it with the data we sought. That could have been the $order_total, but we replaced it with ‘woohoo I found it!’ — pretty silly, huh?
If this is foreign to you, that’s OK. This code is provided to spark ideas. It should be straightforward and simple to implement to a developer who is familiar with WordPress and PHP.
Don’t have a developer? We recommend hiring one! Developers are like car mechanics or plumbers, specialists who are ready to help you out of a pinch and save you time. Get in touch and we will look at your project and offer a flat-rate quote (usually under $100).
Once your custom code snippet is working, it would get pasted in your WP child theme functions.php file or a custom plugin, or added using the WordPress Code Snippets plugin.
Example using Order Data to get Total Price
You can add your own magic tags to PDF Ink for on-the-fly customized watermarks. In this simple example we are going to create a magic tag to reflect a customer’s order total (price) in the watermark, when using a magic tag: [TOTAL_PRICE]
function create_total_price_magic_tag( $content, $key, $order_id, $product_id ) {
$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', 'create_total_price_magic_tag', 10, 4 );
Now when you use [TOTAL_PRICE] in your watermark content, 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.
Use Product Data to Create Magic Tags
In the following example I show how to use the WC $product_id to wheedle out product information for a magic tag which will show the product SKU. Our magic tag will be [ITEM_SKU]. Check it out:
function create_item_sku_magic_tag( $content, $key, $order_id, $product_id ) {
$product = wc_get_product( $product_id );
if ( ! is_a( $product, 'WC_Product' ) ) {
return $content;
}
// Get data 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 should give up
if ( empty( $sku ) ) {
return $content;
}
// When we find [ITEM_SKU] in a watermark, replace it with the product SKU
return preg_replace( '/[ITEM_SKU]/', $sku, $content );
}
add_filter( 'pdfink_filter_placement_content', 'create_item_sku_magic_tag', 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. Want to see more? Check out an example of a custom magic tag for Brazilian CPF checkout fields.