Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woogists/a70e25a570b738f2b0e6c4f5ea11cc65 to your computer and use it in GitHub Desktop.
Save woogists/a70e25a570b738f2b0e6c4f5ea11cc65 to your computer and use it in GitHub Desktop.
[WooCommerce PDF Watermark] Adding More Tags To Text Watermarks
/*
* Snippet to add more tags to text watermarks.
* Code goes in the functions.php file in your theme.
*/
function wc_pdf_watermark_extend_template_tags( $parsed_text, $unparsed_text, $order, $product ) {
// Look for {product_title} in text and replace it with the product title
$parsed_text = str_replace( '{product_title}', $product->get_title(), $parsed_text );
return $parsed_text;
}
add_filter( 'woocommerce_pdf_watermark_parse_template_tags', 'wc_pdf_watermark_extend_template_tags', 10, 4 );
@Dustinvzza
Copy link

Would it be possible to add product title as well as quantity?

@TomasHurtz
Copy link

How to add company name as a tag? I made company name mandatory on the shop and need it included in the watermark

@TomasHurtz
Copy link

OK - I can see

$parsed_text = str_replace( '{billing_company}', $billing_company, $parsed_text );

is available in the class-wc-pdf-watermarker.php file

using {billing_company} as a tag in the settings is fine.

@TomasHurtz
Copy link

Next question: How can we add the product variation name and -> attribute name as a tag?

For example: the PDF product have a dropdown option attribute to select number of licenses to buy for the PDF:

Number of Persons:
1 > 9
10 > 19
etc....

So as a tag, we would like to show: Licensed for " Number of persons": "11 > 30"

@robpl1
Copy link

robpl1 commented Feb 3, 2020

...and the quantity ordered.

@DonQuinleone
Copy link

...and the quantity ordered.

For quantity ordered, you can put this in your functions.php:

function wc_pdf_watermark_extend_template_tags( $parsed_text, $unparsed_text, $order, $product ) {

    foreach ($order->get_items() as $item_id => $item ) {
        $item_quantity  = $item->get_quantity(); // Get the item quantity
    }
	// Look for {quantity} in text and replace it with the quantity
	$parsed_text = str_replace( '{quantity}', $item_quantity, $parsed_text );
	return $parsed_text;
}
add_filter( 'woocommerce_pdf_watermark_parse_template_tags', 'wc_pdf_watermark_extend_template_tags', 10, 4);

Then, use the {quantity} tag within your watermark body.

@joeltsf
Copy link

joeltsf commented Jul 22, 2022

Hi, this is working generally but doesn't seem to return the value per line item in an order with more than one PDF. So the last item in the order gets printed on each PDF. Is there a simple update to this to make sure it pulls the data from each line and applies it to that particular PDF? Thanks!

@jpinostudio128k
Copy link

Hello!
I would like to know if there is the possibility to add the DNI/NIF/CIF field that is added by Art Project Group plugin "WC - APG Campo NIF/CIF/NIE".

This field appears in the invoices and is saved in the customer information, but I don't know with what label I could call it or what code I could add to make it appear in the watermark.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment