PDF template filter hooks

The template engine of PDF Invoices & Packing slips is built for flexibility, and includes a list of filter hooks that you can use to manipulate different types of data.

The template engine of PDF Invoices & Packing slips is built for flexibility, and includes a list of filter hooks that you can use to manipulate different types of data.

HookDescriptionArguments
wpo_wcpdf_listing_actionsFilter PDF orders listing actionsarray $listing_actions, object $order
wpo_wcpdf_meta_box_actionsFilter meta box actions in single order pagearray $meta_box_actions, int $post_id
wpo_wcpdf_bulk_actionsFilter bulk actionsarray $actions
wpo_wcpdf_document_classesFilter document classes – used in admin to load settingsarray $documents
wpo_wcpdf_myaccount_allowed_order_statusesFilter allowed order statusarray $order_status
wpo_wcpdf_myaccount_button_textFilter my account button textstring $button_text, object $invoice
wpo_wcpdf_myaccount_actionsFilter my account actionsarray $actions, object $order
wpo_wcpdf_email_attachment_orderFilter email attachment orderobject $order, object $email, string $document_type
wpo_wcpdf_lock_attachment_fileFilter lock attachment file – default truebool
wpo_wcpdf_reuse_attachment_ageFilter reuse temp file age – default 60 (seconds)int
wpo_wcpdf_attachment_locked_file_delayFilter optional delay (ms) to double check if the write process is finished – default 250 msint
wpo_wcpdf_attach_documentsFilter attach documents for emailarray $attach_documents
wpo_wcpdf_custom_attachment_conditionFilter custom attachment conditionbool = true, object $order, string $email_id, string $document_type
wpo_wcpdf_document_types_for_emailFilter document types for emailarray $document_types, int $email_id, object $order
wpo_wcpdf_check_privsFilter user privileges checkbool $allowed, array $order_ids
wpo_wcpdf_tmp_pathFilter PDF temp folderbool|string $tmp_base
wpo_wcpdf_before_dompdf_renderFilter before DOMPDF renderobject $dompdf, string $html
wpo_wcpdf_after_dompdf_renderFilter after DOMPDF renderobject $dompdf, string $html
wpo_wcpdf_validate_inputFilter settings validate optionsarray $output, array $input
wpo_wcpdf_settings_fields_debugFilter settings fieldsarray $settings_fields, string $page, string $options_group, string $options_name
wpo_wcpdf_template_settings_paper_sizeFilter template paper size settingarray $paper_size
wpo_wcpdf_settings_fields_generalFilter settings fieldsarray $settings_fields, string $page, string $options_group, string $options_name
wpo_wcpdf_template_pathsFilter template pathsarray $template_paths
wpo_wcpdf_templatesFilter templatesarray $installed_templates
wpo_wcpdf_settings_tabsFilter settings tabsarray $settings_tabs
wpo_wcpdf_output_formatFilter output format per document typestring $output_format, string $document_type
wpo_wcpdf_process_order_idsFilter order idsarray $order_ids, string $document_type
wpo_wcpdf_pdf_makerFilter to change the PDF class (which can wrap another PDF library)string $pdf_maker
wcpdf_disable_deprecation_noticesFilter to disable deprecation notices during email sending – default falsebool
wcpdf_log_stacktraceFilter to enable/disable log stack trace – default falsebool
wpo_wcpdf_address_comparison_fieldsFilter address fields for billing and shipping address comparisonarray $address_fields
wpo_wcpdf_ships_to_different_addressBilling and shipping addresses are equal, filter to ship to different addressbool false, object $order, object $order_document
wpo_wcpdf_billing_addressFilter billing addressstring $billing_address, object $order_document
wpo_wcpdf_billing_emailFilter billing emailstring $billing_email, object $order_document
wpo_wcpdf_billing_phoneFilter billing phonestring $billing_phone, object $order_document
wpo_wcpdf_shipping_addressFilter shipping addressstring $shipping_address, object $order_document
wpo_wcpdf_shipping_address_fallbackFilter shipping address to use fallback if no address – default ‘packing-slip’string $document_type, object $order_document
wpo_wcpdf_billing_custom_fieldFilter billing custom fieldstring $custom_field, object $order_document
wpo_wcpdf_dateFilter current datestring $date, object $order_document
wpo_wcpdf_payment_methodFilter payment methodstring $payment_method, object $order_document
wpo_wcpdf_shipping_methodFilter shipping methodstring $shipping_method, object $order_document
wpo_wcpdf_order_numberFilter order numberstring $order_number, object $order_document
wpo_wcpdf_order_dateFilter order datestring $date, string $mysql_date, object $order_document
wpo_wcpdf_order_item_dataFilter order item dataarray $data, object $order, string $document_type
wpo_wcpdf_order_items_dataFilter order items dataarray $data_list, object $order, string $document_type
wpo_wcpdf_calculate_tax_rateFilter to enable tax rate calculation – default falsebool
wpo_wcpdf_calculate_tax_rate_precisionFilter tax rate calculation precisionint|bool
wpo_wcpdf_thumbnail_sizeFilter thumbnail sizestring $thumbnail_size
wpo_wcpdf_use_pathFilter use pathbool
wpo_wcpdf_raw_order_totalsFilter raw order totalsarray $order_item_totals, object $order
wpo_wcpdf_woocommerce_totalsFilter WooCommerce totalsarray $totals, object $order, string $document_type
wpo_wcpdf_order_subtotalFilter order subtotalarray $subtotal, string $tax, string $discount, object $order_document
wpo_wcpdf_order_shippingFilter order shippingarray $shipping, string $tax, object $order_document
wpo_wcpdf_order_discountFilter order discountarray $discount, string $type, string $tax, object $order_document
wpo_wcpdf_order_taxesFilter order taxesarray $taxes, object $order_document
wpo_wcpdf_order_grand_totalFilter order grand totalarray $grand_total, string $tax, object $order_document
wpo_wcpdf_shipping_notesFilter shipping notesstring $shipping_notes, object $order_document
wpo_wcpdf_date_formatFilter date formatstring $date_format, object $order_document
wpo_wcpdf_document_use_historical_settingsFilter to use historical settingsbool $use_historical_settings, object $order_document
wpo_wcpdf_document_store_settingsFilter to use store document settingsbool, object $order_document
wpo_wcpdf_non_historical_settingsFilter non historical settingsarray $non_historical_settings
wpo_wcpdf_document_is_enabledFilter if document is enablebool $enabled, string $document_type
wpo_wcpdf_delete_document_data_keysFilter data to remove from document by data keysarray $document_data_keys
wpo_wcpdf_document_is_allowedFilter for global prevention of creating specific documentbool $allowed, object $order_document
wpo_wcpdf_{$document_slug}_titleFilter document titlestring $title, object $order_document
wpo_wcpdf_document_number_settingsFilter document number settingsarray $number_settings, object $order_document
wpo_wcpdf_template_styles_fileFilter templates styles filestring $template_styles_file
wpo_wcpdf_template_stylesFilter templates stylesstring|bool $css, object $order_document
wpo_wcpdf_header_logo_idFilter header logo idstring $header_logo, object $order_document
wpo_wcpdf_header_logo_img_elementFilter header logo img elementstring $img_element, string $attachment, object $order_document
wpo_wcpdf_{$settings_key}Filter settings text by key (legacy)string $text, object $order_document
wpo_wcpdf_{$settings_key}_settings_textFilter settings text by keystring $text, object $order_document
wpo_wcpdf_load_pdf_file_pathAllow loading of existing PDF file instead of generating on the fly.string $path, object $order_document
wpo_wcpdf_paper_formatFilter document paper format.string $paper_format, string $document_type, object $order_document
wpo_wcpdf_paper_orientationFilter document paper orientation.string $paper_orientation, string $document_type, object $order_document
wpo_wcpdf_get_pdfFilter to get a PDF document.string $pdf, object $order_document
wpo_wcpdf_get_htmlFilter document HTML.string $html, object $order_document
wpo_wcpdf_filenameFilter document filename.string $filename, string $document_type, array $order_ids, string $context
wpo_wcpdf_template_fileFilter document template file.string $file_path, string $document_type, object $order
wpo_wcpdf_wc_emailsFilter WooCommerce emails.array $emails
wpo_wcpdf_merged_bulk_document_contentFilter merged bulk document content.string $html, string $html_content, object $bulk_document
wpo_wcpdf_raw_document_numberFilter the raw document number.int $number, array $settings, object $document, object $order
wpo_wcpdf_formatted_document_numberFilter the formatted document number.string $formatted_number, object $document_number, string $document_type, int $order_id
wpo_wcpdf_format_document_numberFilter on formating the document number.string $formatted_number, object $document_number, object $document, object $order
wpo_wcpdf_external_invoice_number_enabledFilter to enable third-party generated invoice number.bool, object $document_invoice
wpo_wcpdf_external_invoice_numberFilter the third-party generated invoice number.string $invoice_number, object $document_invoice
wpo_wcpdf_document_sequential_number_storeFilter the document sequential number store.string $number_store, object $document_invoice
wpo_wcpdf_settings_fields_documents_invoiceFilter to alter invoice documents settings fields.array $settings_fields, string $page, string $options_group, string $options_name
wpo_wcpdf_settings_fields_documents_packing_slipFilter to alter packing slip documents settings fields.array $settings_fields, string $page, string $option_group, string $option_name
wpo_wcpdf_number_store_table_nameFilter the number store table name.string $table_name, string $store_name, string $method
wpo_wcpdf_invoice_numberFilter the invoice number.string $invoice_number, string $order_number, int $order_id, string $mysql_order_date
wpo_wcpdf_invoice_titleFilter the invoice title.string $title
wpo_wcpdf_packing_slip_titleFilter the packing slip title.string $title
wpo_wcpdf_template_nameFilter the template name.string $template_name, string $template_type
wpo_wcpdf_item_row_classFilter the document item row class.int $item_id, string $document_type, object $order, int $item_id

Using the template filter hooks #

Using filter hooks is not that difficult as it sounds, it’s a simple way for you to manipulate data without dealing with complicated functions or plugin core code. Unlike action hooks, filters always return the first argument data, and because of that you can manipulate that data before returning it. If you never worked with filters you can read this documentation page about how to use filters.

Filter scripts usually live inside functions.php, a file that comes inside any WordPress theme. You can choose to modify that file directly in the main theme (not recommended), within a child-theme or through a plugin like Code Snippets.

Example 1: Sort document order items by category #

This little snippet will sort the document order items by category.

add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_sort_items_by_category', 10, 2 );
function wpo_wcpdf_sort_items_by_category ( $items, $order ) {
    usort($items, function( $a, $b ) {
        return strcmp(wc_get_product_category_list( $a['product']->get_id()), wc_get_product_category_list( $b['product']->get_id()));
    });
 
    return $items;
}

Example 2: Hide specific products on packing slip #

This little snippet will hide specific products (indicated by ID) from the packing slip document.

add_filter( 'wpo_wcpdf_order_item_data', 'wpo_wcpdf_hide_products_packing_slip', 10, 3 );
function wpo_wcpdf_hide_products_packing_slip( $data, $order, $document_type )
{
    $products_ids_to_hide = array(16, 23); // include here your product IDs
 
    if ($document_type == 'packing-slip') { // you can replace with 'invoice' or any other document type
 
        if( in_array($data['product_id'], $products_ids_to_hide) ) {
            $data['name'] = null;
            $data['quantity'] = null;
        }
 
    }
 
    return $data;
}

Example 3: Disallow document on order custom field #

This little snippet will disallow a document creation if a specified custom field is found in the order.

add_filter('wpo_wcpdf_document_is_allowed', 'disallow_document_on_custom_field', 10, 2);
function disallow_document_on_custom_field( $allowed, $document )
{
    $order = $document->order;
 
    if ( !empty($order) && $document->get_type() == 'custom-template' ) { // replace 'custom-template' with your custom template name or default names like 'invoice', 'packing-slip', etc.
 
        $order_meta = $order->get_meta('gift_message'); // replace 'gift_message' with you own custom field name
 
        if( !$order_meta || $order_meta == '' ) {
            $allowed = false;
        }
 
    }
    return $allowed;
}