A customer of mine wanted to use the WooCommerce PDF Invoices & Packing Slips plugin, but wanted to make a small adjustment before he could use the plugin.
WooCommerce PDF Invoices & Packing Slips plugin
This WooCommerce extension automatically adds a PDF invoice to the confirmation email of the order that is sent to your customers. Contains a basic template (additional templates are available via WP Overnight) as well as the possibility to create or modify your own templates. In addition, you can also download or print invoices and packing slips directly from WooCommerce.
My client wanted to attach these PDF invoices, but exclude them from a specific user role. I found the solution in the “wpo_wcpdf_custom_attachment_condition” filter hook in:
wp-content/plugins/woocommerce-pdf-invoices-packing-slips/includes/class-wcpdf-main.php
.
The solution
I created a filter which extracts the customer id from the the $order
(WC_Order object) parameter and then checks the role of that user id.
/**
* Disables attachments for resellers
*/
function wpr_disable_reseller_attachment( $attach, $order, $email_id, $document_type ) {
// Get customer id from order
if ( $customer_id = $order->get_customer_id() ) {
// Get user by customer id
if ( $user = get_user_by( 'id', $user_id ) ) {
// Check if user is reseller or not
return in_array( 'reseller', (array) $user->roles );
}
}
return $attach;
}
add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpr_disable_reseller_attachment', 10, 4 );
In my situation I excluded the “reseller” role, but you can replace this by your own role(s) off course.