WooCommerce PDF Invoice comes with several filter hooks that allow developers to extend functionality.
woo_pdf_generate_regular_invoice
Filter hook called just before generating regular invoice when order is being marked as completed. Simply return false from your callback function to cancel generating invoice.
Parameters:
$proceed | bool | whether to proceed generating invoice or not |
$order | object | current order object |
Example (prevent generating invoices for orders with 0 total):
add_filter('woo_pdf_generate_regular_invoice', 'woo_pdf_generate_regular_invoice_off', 10, 2);
function woo_pdf_generate_regular_invoice_off($bool, $order) {
$order_total = $order->get_total();
if ($order_total == 0) {
return false;
}
return $bool;
}
woo_pdf_send_by_email
Filter hook called just before attaching PDF invoice to one of standard WooCommerce emails. Simply return false from your callback function to cancel generating invoice.
Parameters:
$proceed | bool | whether to proceed generating invoice or not |
$order | object | current order object |
$email_type | string | WooCommerce email type, e.g. "customer_completed_order" |
woo_pdf_allow_regular_invoice_download
Filter hook called just before displaying regular invoice download link or download button. Simply return false from your callback function to cancel generating invoice.
Parameters:
$display | bool | whether to proceed displaying regular invoice download link or not |
$order | object | current order object |
$location | string | where download link is displayed ("single" - single order view; "list" - order list view) |
woo_pdf_allow_proforma_invoice_download
Filter hook called just before displaying proforma invoice download link or download button. Simply return false from your callback function to cancel generating invoice.
Parameters:
$display | bool | whether to proceed displaying proforma invoice download link or not |
$order | object | current order object |
$location | string | where download link is displayed ("single" - single order view; "list" - order list view) |
woo_pdf_prefix_suffix_macros
Filter hook used to add additional macros to be used in invoice number prefix and suffix. Push additional elements to $macros array (macro key as array item key, replacement value as array value) and return it.
Parameters:
$macros | array | contains invoice number prefix/suffix macros and their values |
$order | object | current order object |
$position | string | "prefix" or "suffix" |
woo_pdf_macros
Filter hook used to add additional macros to be used in footer and custom content blocks. Push additional elements to $macros array (macro key as array item key, replacement value as array value) and return it.
Parameters:
$macros | array | contains macros and their values |
$order | object | current order object |
Examples:
Just a simple "fixed" value:
add_filter('woo_pdf_macros', 'woo_pdf_custom_macros', 10, 2);
function woo_pdf_custom_macros($macros, $order)
{
$custom_macro_key = '{{key}}';
$custom_macro_value = 'key';
$macros[$custom_macro_key] = $custom_macro_value;
return $macros;
}
add_filter('woo_pdf_macros', 'woo_pdf_custom_macro_order_status', 10, 2);
function woo_pdf_custom_macro_order_status($macros, $order)
{
$custom_macro_key = '{{order_status}}';
$custom_macro_value = $order->get_status();
$macros[$custom_macro_key] = $custom_macro_value;
return $macros;
add_filter('woo_pdf_macros', 'woo_pdf_custom_macros', 10, 2);
function woo_pdf_custom_macros($macros, $order)
{
// Configure the parameters
$custom_macro_key = '{{key}}';
$custom_macro_value = 'no value'; // default value
$checkout_field_key = 'test'; // fill in the correct unique key here
// Check if order is actually an object
if (is_object($order)) {
// Get order id correctly
$id = $order->get_order_number();
$custom_macro_value = wccf_get_values(array(
'key' => $checkout_field_key,
'context' => 'checkout_field',
'item_id' => $id,
'include_meta' => false
));
}
$macros[$custom_macro_key] = $custom_macro_value;
return $macros;
}
woo_pdf_styles
Filter hook called every time some string changes styles - font size, style and color. You can override any of that and change old values to new (examples below).
Parameters:
$styles | array | default array of styles |
$order | object | current order object |
Examples:
1 - Changing font color:
add_filter('woo_pdf_styles', 'change_invoice_grey_color');
function change_invoice_grey_color($styles)
{
if (isset($styles['color']) && $styles['color'] === '#808080') {
$styles['color'] = '#000000';
}
return $styles;
}
2 - Changing font size:
add_filter('woo_pdf_styles', 'change_invoice_font_size');
function change_invoice_font_size($styles)
{
if (isset($styles['size']) && $styles['size'] === 9 ) { $styles['size'] = 8;
}
return $styles;
}
add_filter('woo_pdf_styles', 'change_invoice_font');
function change_invoice_font($styles)
{
$styles['family'] = 'helvetica';
return $styles;
}
woo_pdf_totals
Filter hook called just before displaying the totals block, and can be used to change the data, remove or add any values or just change the order of the totals.
Parameters:
$totals | array | default array of totals |
$order | object | current order object |
$options | array | array of options |
Example of simple rearranging of the old totals:
add_filter('woo_pdf_totals', 'woo_pdf_change_totals');
function woo_pdf_change_totals($totals) {
$new_totals = array(
'subtotal' => $totals['subtotal'],
'taxes' => $totals['taxes'],
'shipping' => $totals['shipping'],
'total' => $totals['total'],
);
return $new_totals;
}