Actions & Filters

Permalink Actions & Filters

The PDF Invoice add-on offers actions and filters which enable developers to access the code behind the add-on. The following section lists all actions and filters along with corresponding examples:

Permalink class-wp-wc-invoice-pdf-backend-options.php

Name: woocommerce_get_sections_preferences-wp-wc-invoice-pdf
Type: Filter
Description: You can edit the Sections option in the backend, hide sections and add your own sections.

Example:


add_filter( 'woocommerce_get_sections_preferences-wp-wc-invoice-pdf', 'my_invoice_sections_preferences' );
function my_invoice_sections_preferences( $sections ) {
	unset( $sections[ 'custom_css_styles' ] );		// remove 'Custom CSS Styles'
	$sections[ 'my_section' ] = 'My Section';		// add section 'My Section'
	return $sections;
}

Name: wp_wc_invoice_pdf_options_section_{{section_id}}
Type: Filter
Description: If you have added your own Sections option you can use this filter to add option fields. You can also add option fields for existing sections. You can use your options in template invoice-content.php.

Example:

// add an options to custom section 'my_section'
add_filter( 'wp_wc_invoice_pdf_options_section_my_section', 'my_invoice_options_section_my_section' );
function my_invoice_options_section_my_section( $options ) {
	$options[] = array( 'title'	=> 'My Settings', 'type' => 'title','desc' => '', 'id' => 'wp_wc_invoice_pdf_my_settings' );
	$options[] = array(
					'name' 		=> 'My Setting',
					'desc' 		=> '<br />' . 'My description',
					'tip'  		=> 'My tip',
					'id'   		=> 'wp_wc_invoice_pdf_my_setting',
					'type' 		=> 'text',
					'default'  	=> 'My default'
				);
	$options[] = array( 'type' => 'sectionend', 'id' => 'wp_wc_invoice_pdf_my_settings' );
	return $options;
}
// add an option to section ivoice_content
add_filter( 'wp_wc_invoice_pdf_options_section_invoice_content', 'my_invoice_options_section_invoice_content' );
function my_invoice_options_section_invoice_content( $options ) {
	$options[] = array( 'title'	=> 'My Content Settings', 'type' => 'title','desc' => '', 'id' => 'wp_wc_invoice_pdf_my_content_settings' );
	$options[] = array(
					'name' 		=> 'My Color',
					'desc' 		=> '<br />' . 'My color',
					'tip'  		=> 'My tip',
					'id'   		=> 'wp_wc_invoice_pdf_my_color',
					'type' 		=> 'color',
					'default'  	=> '#ff0000'
				);
	$options[] = array( 'type' => 'sectionend', 'id' => 'wp_wc_invoice_pdf_my_content_settings' );
	return $options;
}
// add an option to section emails
add_filter( 'wp_wc_invoice_pdf_options_section_emails', 'my_invoice_options_section_emails' );
function my_invoice_options_section_emails( $options ){
	$last_element = array_pop( $options );
	$options[] = array(	
					'name' => 'Kundennotiz',
					'desc' => '<br />' . 'Add invoice as an attachment to "Customer note" email',
					'tip'  => 'Add invoice as an attachment to "Customer note" email',
					'id'   => 'wp_wc_invoice_pdf_emails_customer_note',
					'type' => 'select',
					'default'  => 'off',
					'options' => array(
									'on'  => __( 'Yes', 'woocommerce-invoice-pdf' ),
									'off' => __( 'No', 'woocommerce-invoice-pdf' )
								)	
				);		
	$options[] = $last_element;	
	return $options;
}

Permalink Placeholders in options

Name: wp_wc_invoice_pdf_placeholders
Files: class-wp-wc-invoice-pdf-create-pdf.php, class-wp-wc-invoice-pdf-backend-options.php, options-section-general-pdf-settings.php, options-section-invoice-content.php, invoice-content.php
Type: Filter
Description: It is possible to add placeholders that can be used for frontend filenames, backend filenames and the subject line in the invoice.

Example:

add_filter( 'wp_wc_invoice_pdf_placeholders', 'my_placeholder' );
function my_placeholder( $placeholders ) {
	$placeholders[ 'customer-name' ]	= 'Customer's last name';
	$placeholders[ 'my-random-number' ]	= 'My Random Number';
	return $placeholders;	
}

Name: wp_wc_invoice_pdf_placeholder_{{placeholder}}
Files: class-wp-wc-invoice-pdf-create-pdf.php, invoice-content.php
Type: Filter
Description: You can create a filter defining what each of the placeholders added is to be replaced with. The default setting is for the placeholder to be replaced by the value you set in filter “wp_wc_invoice_pdf_placeholders”.

Example:

add_filter( 'wp_wc_invoice_pdf_placeholder_my-random-number', 'my_random_number', 10, 3 );
function my_random_number( $value, $key, $order ) {
	return rand( 100, 999 );	// or some useful meta data of order
}
add_filter( 'wp_wc_invoice_pdf_placeholder_customer-name', 'my_customer_name', 10, 3 );
function my_customer_name( $value, $key, $order ) {
	if ( is_a( $order, 'WC_Order' ) ) {
		return $order->billing_last_name;
	} else {
		return __( 'Doe', 'woocommerce-invoice-pdf' );
	}
}

Permalink class-wp-wc-invoice-pdf-backend-download.php

Name: wp_wc_invoice_pdf_before_backend_download
Type: Action
Description: Carry out an action before initiating a download in the backend. Do not create an output; if you do you will receive the error message “Unable to stream pdf: headers already sent”.

Example:

add_action( 'wp_wc_invoice_pdf_before_backend_download', 'my_invoice_before_backend_download', 10, 1 ); 
function my_invoice_before_backend_download( $order ) {
	// do sth
} 

Name: wp_wc_invoice_pdf_backend_filename
Type: Filter
Description: This filter can be used to change the backend filename.

Example:

add_filter( 'wp_wc_invoice_pdf_backend_filename', 'my_invoice_backend_filename', 10 ,2 );
function my_invoice_backend_filename( $filename, $order ) {
	return date( 'Y-m-d-H-i' ) . '-' . $filename;
}

Name: wp_wc_invoice_pdf_test_filename
Type: Filter
Description: This filter can be used to change the filename for test invoices.

Example:

add_filter( 'wp_wc_invoice_pdf_test_filename', 'my_invoice_test_filename' );
function my_invoice_test_filename( $filename ) {
	return date( 'Y-m-d-H-i' ) . '-' . $filename;
}

Permalink class-wp-wc-invoice-pdf-view-order-download.php

Name: wp_wc_invoice_pdf_before_frontend_download
Type: Action
Description: Carry out an action before initiating a download in the frontend. Do not create an output; if you do you will receive the error message “Unable to stream pdf: headers already sent”.

Example:

add_action( 'wp_wc_invoice_pdf_before_frontend_download', 'my_invoice_before_frontend_download', 10, 1 ); 
function my_invoice_before_frontend_download( $order ) {
	// do sth
} 

Name: wp_wc_invoice_pdf_view_order_button
Type: Action
Description: This action enables you to influence the way the download button is displayed in the frontend.

Example:

add_action( 'wp_wc_invoice_pdf_view_order_button', 'my_view_order_button', 10, 4 );
function my_view_order_button( $a_href, $a_target, $a_attributes, $button_text ) {
	?>
	<p class="my-invoice-button-class">
		<a href="<?php echo $a_href; ?>" class="button" >
		Download	
		</a>
	</p>
	<?php>
}

Name: wp_wc_invoice_pdf_frontend_filename
Type: Filter
Description: This filter can be used to change the frontend filename.

Example:

add_filter( 'wp_wc_invoice_pdf_frontend_filename', 'my_invoice_frontend_filename', 10, 2 );
function my_invoice_frontend_filename( $filename, $order ) {
	if ( current_user_can( 'install_plugins' ) ) {
		return 'my-own-invoice-' . $filename;	
	} else {
		return $filename;	
	}
}

Name: wp_wc_invoice_pdf_view_order_redirect_link
Type: Filter
Description: This filter is used to define which page the user is redirected to if they are not permitted to retrieve the PDF. The default setting is for them to be redirected to the My Account page.

Example:

add_filter( 'wp_wc_invoice_pdf_view_order_redirect_link', 'my_invoice_view_order_redirect_link' );
function my_invoice_view_order_redirect_link( $location ) {
	return get_permalink( 268 );
}

Permalink class-wp-wc-invoice-pdf-email-attachment.php

Name: wp_wc_invoice_before_adding_attachment
Type: Action
Description: Carry out an action before the invoice is attached to an email.

Example:

add_action( 'wp_wc_invoice_before_adding_attachment', 'my_invoice_before_adding_attachment', 10, 2 );
function my_invoice_before_adding_attachment( $status, $order ) {
	// do sth.
}

Name: wp_wc_inovice_pdf_allowed_stati
Type: Filter
Description: This filter makes it possible to attach invoices to emails to other email addresses registered in the WooCommerce system. This also requires the addition of these statuses to the Options menu.

Example:

add_filter( 'wp_wc_inovice_pdf_allowed_stati', 'my_invoice_allowed_stati' );
function my_invoice_allowed_stati( $allowed_stati ) {
	$allowed_stati[] = 'customer_note';
	return $allowed_stati;
}

Permalink invoice-content.php

Name: wp_wc_invoice_pdf_invoice_date
Type: Filter
Description: This filter can be used to add an invoice date to the invoice.

Example:

add_filter( 'wp_wc_invoice_pdf_invoice_date', 'my_invoice_invoice_date', 10, 2 );
function my_invoice_invoice_date( $invoice_date, $order ) {
	return 'Invoice date:<br />' . date( 'Y.m.d' ); // or add some meta data	
}

Permalink class-wp-wc-invoice-pdf-create-pdf.php

Name: wp_wc_invoice_pdf_before_get_pdf_content
Type: Filter
Description: This filter can be used to carry out an action before the content of the invoice is created.

Example:

add_action( 'wp_wc_invoice_pdf_before_get_pdf_content', 'my_invoice_before_get_pdf_content', 10 ,2 );
function my_invoice_before_get_pdf_content( $order, $args ) {
	// do sth	
}

Name: wp_wc_invoice_pdf_output_format
Type: Filter
Description: In test and development scenarios it can be helpful to render the invoice in HTML rather than in PDF format. In this case the filter must return the string “html”.

Example:

add_filter( 'wp_wc_invoice_pdf_output_format', 'my_invoice_output_format' );
function my_invoice_output_format( $output_format ) {
	return 'html';	
}

Name: wp_wc_invoice_pdf_args_before_pdf_rendering
Type: Filter
Description: You can change $args before the PDF is rendered; this is useful in test scenarios.

Example:

add_filter( 'wp_wc_invoice_pdf_args_before_pdf_rendering', 'my_invoice_args_before_pdf_rendering' );
function my_invoice_args_before_pdf_rendering( $args ) {
	$args[ 'remove_css_style' ] = true;
	$args[ 'inline_style' ] = false;
	return $args;
}

Name: wp_wc_invoice_get_footer_or_header, wp_wc_invoice_include_background_image, wp_wc_invoice_get_fonts
Type: Filter
Description: These filters offer full control over the output. If a filter is applied the standard code will not be executed.

Example:

add_filter( 'wp_wc_invoice_get_footer_or_header', 'my_invoice_get_footer_or_header', 10, 3 );
function my_invoice_get_footer_or_header( $content, $part, $args ) {
	if ( $part == 'header' ) {
		// $content = ...	
	} else {
		// $content = ...
	}
	return $content;
}
add_filter( 'wp_wc_invoice_include_background_image', 'my_invoice_include_background_image', 10, 5 );
function my_invoice_include_background_image( $content, $part, $height, $width, $args ) {
	// $content = ... // your code
	return $content;	
}
add_filter( 'wp_wc_invoice_get_fonts', 'my_invoice_get_fonts', 10, 2 );
function my_invoice_get_fonts( $content, $args ) {
	// $content = ... // your code
	return $content;	
}

Name: wp_wc_invoice_pdf_template_invoice_content, wp_wc_invoice_pdf_template_default_styles, wp_wc_invoice_pdf_template_page_numbers
Type: Filter
Description: These three template files are used to create the content of the invoice. Templates “invoice-content.php” and “invoice-default-styles.php” can be overwritten by theme templates. Template “invoice-content-page-numbers.php” controls the output of page numbers, does not include any HTML code and cannot be overwritten by a theme template. The following filters can be used to load other files for all three templates.

Example:

add_filter( 'wp_wc_invoice_pdf_template_invoice_content', 'my_invoice_template_invoice_content' ); 
function my_invoice_template_invoice_content( $template_file ) {
	$my_template_file = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'my_template_invoice_content.php';	 
	return ( file_exists( $my_template_file ) ) ? $my_template_file : $template_file;
}

Permalink invoice-content-page-numbers.php

Name: wp_wc_invoice_pdf_page_number_text_height_factor
Type: Filter
Description: If your page numbers do not appear to be in exactly the right place you can change the following variable in order to exert more control over the height of the text. The standard value is 0.87.

Example:

add_filter( 'wp_wc_invoice_pdf_page_number_text_height_factor', 'my_invoice_page_number_text_height_factor' );
function my_invoice_page_number_text_height_factor( $factor ) {
	return 0.9;
}

Permalink If you would prefer the add-on to use the polite “Sie” form when addressing you

add_filter( 'load_textdomain_mofile', 'wp_wc_invoice_pdf_language_de_sie', 10, 2 );
function wp_wc_invoice_pdf_language_de_sie( $mofile, $domain ) {
    if ( 'woocommerce-invoice-pdf' === $domain ) {
		return str_replace( '-de_DE', '-de_DE_Sie', $mofile );
	}
    return $mofile;
}