WooCommerce PDF Watermark: Using custom order item meta data WooCommerce PDF Watermark: Using custom order item meta data wordpress wordpress

WooCommerce PDF Watermark: Using custom order item meta data


Try the following instead (where I use the 4th variable function argument $product to target the current product from order items):

add_filter( 'woocommerce_pdf_watermark_parse_template_tags', 'wc_pdf_watermark_extend_template_tags', 10, 4 );function wc_pdf_watermark_extend_template_tags( $parsed_text, $unparsed_text, $order, $product ) {    // Loop through order items    foreach ( $order->get_items() as $item_id => $item ) {        // Target only current product        if( in_array( $product->get_id(), [$item->get_product_id(), $item->get_variation_id()] ) ) {            $product_id  = $item->get_product_id();            $licensed_to = $item->get_meta('licensed_to');            $replacement = $product_id . ' - ' . $item_id . ' - ' . $licensed_to;            $parsed_text = str_replace( '{licensed_to}', $replacement, $parsed_text );        }    }    return $parsed_text;}

Code goes in functions.php file of the active child theme (or active theme). It could work.


Maybe (not sure), you could try to replace the following line:

 $parsed_text = str_replace( '{licensed_to}', $replacement, $parsed_text );

with:

 $parsed_text = str_replace( '{licensed_to}', $replacement, $unparsed_text );