User Role
-
Can you tell me how to add the user’s role to the packing slip? I need to differentiate between my customers and my wholesale customers.
Thanks!
-
Hi @creoadmin,
You could use the following action hook:
add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_print_user_role', 10, 2 ); function wpo_wcpdf_print_user_role ($template_type, $order) { if ($template_type == 'packing-slip') { $user = $order->get_user(); if ( $user && in_array( 'wholesale_customer', (array) $user->roles ) ) { ?> <tr class="user-role"> <th>User role:</th> <td>Wholesale customer</td> </tr> <?php } else { ?> <tr class="user-role"> <th>User role:</th> <td>Regular customer</td> </tr> <?php } } }You can add this in the functions.php of your child-theme. If you have never worked with action hooks or functions.php please read this first: How to use filters
I hope this helps.
With kind regards,
Michael
Thanks! I have tried adding this using code snippet, but receiving this error:
The snippet has been deactivated due to an error on line 2:syntax error, unexpected ‘function’ (T_FUNCTION).
I also tried adding directly to php but it caused my site to break.
Ideas? Thanks so much!!
Hello @creoadmin,
I just tested this too, and it works fine for me. Is it possible that there’s a copy paste error? Sometimes browsers try to be ‘smart’ and change regular apostrophes for left and right apostrophies, but that’s not the same in PHP. You could also try this, which is the same code but in ‘raw’ format:
https://pastebin.com/raw/5nVWCGK6Let us know if that works for you!
Ewout
That worked PERFECTLY!!! Thank you a bunch!
So…I thought this was working perfect. 🙁 It is putting the user role on the packing slip, but not reading the correct user role. Everybody is just listed as regular customer.
What do you see when you print the user roles?
so:<td><<?php echo $user ? implode(', ', (array) $user->roles ) : 'not a user'; ?> </td>instead of:
<td>Regular customer</td>All of my users are listed as regular customer. Some of them should be listed as “Wholesale Tax Free” because that is the role that I have assigned to them.
I’m not sure I understand… Did you try replacing that snippet? What did that output?
OH sorry! I didn’t know you were saying replace that. Should I add that somewhere to the original code you gave me? Or, use the new snippet and delete the other?
You can just replace it in the original code as instructed above. Let us know what you find!
The shows:
User Role: (blank)
It is now not showing the user role of any customer or wholesale customer.
That is very strange… Can you enable debug output in the Status tab and then try again (turning it off again after testing unless this is on a staging site)?
That is still showing nothing for user role. It is blank.
I’m a bit lost here, could you post the entire snippet you’re currently using?
You bet!
add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_print_user_role', 10, 2 );
function wpo_wcpdf_print_user_role ($template_type, $order) {
if ($template_type == 'packing-slip') {
$user = $order->get_user();
if ( $user && in_array( 'wholesale_customer', (array) $user->roles ) ) {
?>
<tr class="user-role">
<th>User role:</th>
<td>Wholesale customer</td>
</tr>
<?php
} else {
?>
<tr class="user-role">
<th>User role:</th>
<td>Regular customer</td>
</tr>
<?php
}
}
}
The topic ‘User Role’ is closed to new replies.