Home / eCommerce / Exclude Wholesale Customers from receiving the PDF Invoice
Duplicate Snippet

Embed Snippet on Your Site

Exclude Wholesale Customers from receiving the PDF Invoice

Integration with the PDF Invoices & Packing Slips for WooCommerce plugin by WP Overnight.

Code Preview
php
<?php
/**
 * Exclude wholesale roles from receiving the PDF Invoice attachment in emails.
 *
 * Wholesale Suite roles on this site:
 *   - wholesale_customer (Wholesale Customer)
 *   - wholesale_vip (Wholesale VIP)
 *
 * Plugin: WooCommerce PDF Invoices & Packing Slips
 * Hook: wpo_wcpdf_document_is_allowed
 */
add_filter( 'wpo_wcpdf_document_is_allowed', function( $allowed, $document ) {
    // Only target the Invoice document type
    if ( 'invoice' !== $document->get_type() ) {
        return $allowed;
    }
    $order = $document->order;
    if ( ! $order instanceof WC_Order ) {
        return $allowed;
    }
    $customer_id = $order->get_customer_id();
    if ( ! $customer_id ) {
        return $allowed; // Guest order — no role to check
    }
    $wholesale_roles = [
        'wholesale_customer',
        'wholesale_vip',
    ];
    $user = get_userdata( $customer_id );
    if ( $user && array_intersect( $wholesale_roles, (array) $user->roles ) ) {
        return false; // Block the invoice attachment for wholesale users
    }
    return $allowed;
}, 10, 2 );

Comments

Add a Comment