Back to Community

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_log or 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 deprecated get_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