How to Conditionally Control Content in Your WooCommerce PDF Invoices
Content
One of the most common challenges when using the 'PDF Invoices & Packing Slips for WooCommerce' plugin is controlling exactly what appears in your documents. Whether you need to hide empty fields, exclude specific products, or prevent invoice generation for certain payment methods, conditional logic is key. This guide covers the most effective hooks and techniques to gain precise control over your PDF content.
Understanding the Core Hooks for Conditional Logic
The plugin provides several powerful hooks that allow you to manipulate content based on conditions. The most frequently used ones are:
wpo_wcpdf_document_is_allowed: Controls whether a document (like an invoice) is generated at all.wpo_wcpdf_custom_attachment_condition: Controls whether a generated PDF is attached to an email.wpo_wcpdf_custom_styles: Adds CSS to hide or show elements conditionally.wpo_wcpdf_order_items_data: Filters the line items that appear in the document.
Common Scenarios and Solutions
1. Hiding Empty or Unwanted Custom Fields
If you've added a custom field but don't want it to display when empty, you need to wrap your output code in a proper conditional check. The key is to retrieve the field's value first and then check if it's not empty before rendering the HTML.
add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_purchase_order', 10, 2 );
function wpo_wcpdf_purchase_order ($document_type, $order) {
$document = wcpdf_get_document( $document_type, $order );
$purchase_order = $document->custom_field('purchase_order'); // Get the value first
// Only display if the value exists and is not empty
if (!empty($purchase_order)) {
?>
<tr class="purchase-order">
<th>Purchase Order:</th>
<td><?php echo $purchase_order; ?></td>
</tr>
<?php
}
}
2. Preventing Invoice Generation Based on Payment Method
To completely stop an invoice from being created for orders with a specific payment method (like 'cod' for Cash on Delivery or a custom wallet system), use the wpo_wcpdf_document_is_allowed filter. This is more efficient than generating the document and then not attaching it.
add_filter( 'wpo_wcpdf_document_is_allowed', 'wpo_wcpdf_disable_invoice_for_payment_method', 10, 2 );
function wpo_wcpdf_disable_invoice_for_payment_method( $allowed, $document ) {
if ( ! empty( $document->order ) && 'invoice' === $document->get_type() ) {
$order = $document->order;
$payment_method = is_callable( array( $order, 'get_payment_method' ) ) ? $order->get_payment_method() : '';
// Disallow for 'cod' and 'wallet'
if ( in_array( $payment_method, array( 'cod', 'wallet' ) ) ) {
$allowed = false;
}
}
return $allowed;
}
3. Controlling PDF Email Attachments
If you still want the invoice to be generated and stored in the order, but don't want it emailed to the customer for certain conditions, use the wpo_wcpdf_custom_attachment_condition filter. This is useful if you need to manually send the invoice later.
add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_skip_attachment_for_cod', 10, 4 );
function wpo_wcpdf_skip_attachment_for_cod( $condition, $order, $email_id, $document_type ) {
if ( ! empty( $order ) && 'invoice' == $document_type ) {
$payment_method = is_callable( array( $order, 'get_payment_method' ) ) ? $order->get_payment_method() : '';
if ( 'cod' == $payment_method ) {
$condition = false; // Do not attach the PDF
}
}
return $condition;
}
4. Hiding Specific Line Items or Product Categories
To remove entire products from the invoice or packing slip—for example, products from a 'noinvoice' category or parent bundle products—you need to filter the order items data.
add_filter('wpo_wcpdf_order_items_data', 'wpo_wcpdf_remove_items_by_category', 10, 3);
function wpo_wcpdf_remove_items_by_category($items, $order, $document_type) {
$categories_to_remove = array( 'noinvoice' );
foreach ($items as $item_id => $item) {
$product = $item['product'];
if ( $product ) {
$product_categories = wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'slugs' ) );
// Check if the item's categories intersect with the categories to remove
if ( ! empty( array_intersect( $product_categories, $categories_to_remove ) ) ) {
unset($items[$item_id]); // Remove the item
}
}
}
return $items;
}
Important Tips for Success
- Check Your Hooks: Using the wrong hook is a common source of failure. Decide if you need to prevent document creation (
document_is_allowed), prevent email attachment (custom_attachment_condition), or just hide the visual output (custom_styles). - Test Your Conditions: Always use
error_logor a debugging plugin to verify that your conditional checks (like$order->get_payment_method()) are returning the expected values. - Use Correct Order Methods: When accessing order data, use modern methods like
$order->get_payment_method()instead of the deprecatedget_post_meta($order->id, '_payment_method', true).
By understanding and correctly applying these filters, you can tailor your PDF invoices and packing slips to meet any business requirement, ensuring your documents are always clear, professional, and contain only the necessary information.
Related Support Threads Support
-
Hide empty custom field from Invoicehttps://wordpress.org/support/topic/hide-empty-custom-field-from-invoice/
-
Excluir categoría de producto en facturahttps://wordpress.org/support/topic/excluir-categoria-de-producto-en-factura/
-
Disable PDF invoice generation with Wallet rechargeable productshttps://wordpress.org/support/topic/disable-pdf-invoice-generation-with-wallet-rechargeable-products/
-
Remove a custom field from the PDF invoicehttps://wordpress.org/support/topic/remove-a-custom-field-from-the-pdf-invoice/
-
Hide parent bundle products but keep child productshttps://wordpress.org/support/topic/hide-parent-bundle-products-but-keep-child-products/
-
Exclude product category from invoicehttps://wordpress.org/support/topic/exclude-product-category-from-invoice/
-
Adding Free Delivery Minimum Order Amounthttps://wordpress.org/support/topic/adding-free-delivery-minimum-order-amount/
-
Exclude wpo_wcpdf_payment_method for one producthttps://wordpress.org/support/topic/exclude-wpo_wcpdf_payment_method-for-one-product/
-
Hide and Show Payment Method from invoicehttps://wordpress.org/support/topic/hide-and-show-payment-method-from-invoice/
-
Sending an invoice only when the checkbox is checked – problemhttps://wordpress.org/support/topic/sending-an-invoice-only-when-the-checkbox-is-checked-problem/
-
Add item to invoice with negative priccehttps://wordpress.org/support/topic/add-item-to-invoice-with-negative-pricce/
-
Custom template doesn’t show up in general settingshttps://wordpress.org/support/topic/custom-template-doesnt-show-up-in-general-settings/
-
remove attachment for codhttps://wordpress.org/support/topic/remove-attachment-for-cod/
-
Generate PDF Only for “con_fattura” Payment Methodhttps://wordpress.org/support/topic/generate-pdf-only-for-con_fattura-payment-method/
-
Hide Tax and subtotal for customers outside of store billing countryhttps://wordpress.org/support/topic/hide-tax-for-customers-outside-of-store-billing-country/
-
change PDF pricehttps://wordpress.org/support/topic/change-pdf-price/
-
hidden field with show in cart unchecked displayed in invoicehttps://wordpress.org/support/topic/hidden-field-with-show-in-cart-unchecked-displayed-in-invoice-2/