• Resolved creoadmin

    (@creoadmin)


    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!

Viewing 15 replies - 1 through 15 (of 20 total)
  • Plugin Contributor kluver

    (@kluver)

    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

    Thread Starter creoadmin

    (@creoadmin)

    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!!

    Plugin Contributor Ewout

    (@pomegranate)

    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/5nVWCGK6

    Let us know if that works for you!

    Ewout

    Thread Starter creoadmin

    (@creoadmin)

    That worked PERFECTLY!!! Thank you a bunch!

    Thread Starter creoadmin

    (@creoadmin)

    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.

    Plugin Contributor Ewout

    (@pomegranate)

    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>
    
    Thread Starter creoadmin

    (@creoadmin)

    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.

    Plugin Contributor Ewout

    (@pomegranate)

    I’m not sure I understand… Did you try replacing that snippet? What did that output?

    Thread Starter creoadmin

    (@creoadmin)

    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?

    Plugin Contributor Ewout

    (@pomegranate)

    You can just replace it in the original code as instructed above. Let us know what you find!

    Thread Starter creoadmin

    (@creoadmin)

    The shows:

    User Role: (blank)

    It is now not showing the user role of any customer or wholesale customer.

    Plugin Contributor Ewout

    (@pomegranate)

    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)?

    Thread Starter creoadmin

    (@creoadmin)

    That is still showing nothing for user role. It is blank.

    Plugin Contributor Ewout

    (@pomegranate)

    I’m a bit lost here, could you post the entire snippet you’re currently using?

    Thread Starter creoadmin

    (@creoadmin)

    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
    }
    }
    }

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘User Role’ is closed to new replies.