I’ve created a custom WooCommerce checkout field with Woothemes Checkout Field Editor labeled “po_number”. I would like the PO Number checkout field to only display for the user role “distributor”.
So far I’ve been unsuccessful in overriding the checkout fields. I’m using WordPress 4.5.1 / Woocommerce 2.5.5. Here’s the code I’ve placed in my child theme’s functions.php
. I’ve also tested to make sure it is not a theme conflict.
Any help is greatly appreciated.
This is my code:
function custom_override_checkout_fields( $fields ) {
if ( ! current_user_can( 'distributor' ) && isset( $fields['billing']['po_number'] ) ) {
unset($fields['billing']['po_number']);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
The
current_user_can()
function is related to capabilities of the user roles, but not to detect the user roles themselves. For that reason is not working in your code.You need to set a conditional function for this purpose (user roles):
Then in your code you can use that function:
This should work in your code.