I've created a user role of CSR and several custom checkout fields to appear on the Checkout page of WooCommerce, and I want to hide these checkout fields from any other user but those with the CSR role.
I've created the fields and the role, but something is off with my fields as they're still showing up for all users. I followed the tutorial here to hide the fields. Apologies if the formatting of the code is off. The editor didn't accept most of my formatting when I pulled it in from Atom.
ADD CUSTOM FIELDS
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div class="my_custom_checkout_field"><h2>' . __('CSR Information')
.'</h2>';
woocommerce_form_field( 'date_of_purchase', array(
'type' => 'text',
'label' => __('Date of Purchase', 'woocommerce'),
'placeholder' => _x('MM/DD/YYYY', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
), $checkout->get_value( 'date_of_purchase' ));
woocommerce_form_field( 'place_of_purchase', array(
'type' => 'select',
'label' => __('Place of Purchase', 'woocommerce'),
'placeholder' => _x('Select Option', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
), $checkout->get_value( 'place_of_purchase' ));
woocommerce_form_field( 'color_item', array(
'type' => 'select',
'label' => __('Product Color', 'woocommerce'),
'placeholder' => _x('Select Option', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
), $checkout->get_value( 'color_item' ));
woocommerce_form_field( 'product_model', array(
'type' => 'select',
'label' => __('Model', 'woocommerce'),
'placeholder' => _x('Select Option', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
), $checkout->get_value( 'product_model' ));
echo '<strong>' . __('Check All That Apply:') .'</strong>';
woocommerce_form_field( 'lightbulb_out', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Lightbulb is Out'),
'required' => false,
), $checkout->get_value( 'lightbulb_out' ));
woocommerce_form_field( 'not_turn_on', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Will Not Turn On'),
'required' => false,
), $checkout->get_value( 'not_turn_on' ));
woocommerce_form_field( 'fan_not_running', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Fan Stopped Running'),
'required' => false,
), $checkout->get_value( 'fan_not_running' ));
woocommerce_form_field( 'strange_noise', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Strange Noise'),
'required' => false,
), $checkout->get_value( 'strange_noise' ));
woocommerce_form_field( 'not_catching', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Not Catching Insects'),
'required' => false,
), $checkout->get_value( 'not_catching' ));
woocommerce_form_field( 'csr_other', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Other'),
'required' => false,
), $checkout->get_value( 'csr_other' ));
woocommerce_form_field( 'case_description', array(
'type' => 'textarea',
'label' => __('Description of Case', 'woocommerce'),
'placeholder' => _x('Please provide details', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
), $checkout->get_value( 'case_description' ));
echo '</div>';
}
ADD CSR ROLE
$result = add_role( 'csr', __('CSR' ),
array(
'read' => true, // true allows this capability
'edit_posts' => false, // Denies user to edit their own posts
'edit_pages' => false, // Denies user to edit pages
'edit_others_posts' => false, // Denies user to edit others posts not just
their own
'create_posts' => false, // Denies user to create new posts
'manage_categories' => false, // Denies user to manage post categories
'publish_posts' => false, // Denies the user to publish, otherwise posts stays
in draft mode
'edit_themes' => false, // false denies this capability. User can’t edit your
theme
'install_plugins' => false, // User cant add new plugins
'update_plugin' => false, // User can’t update any plugins
'update_core' => false // user cant perform core updates
)
);
HIDE CSR VALUES FOR ALL BUT CSR
function custom_override_checkout_fields( $fields ) {
if ( ! current_user_can( 'csr' ) && isset( $fields['date_of_purchase'] ) ) {
unset( $fields[['date_of_purchase']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['place_of_purchase'] ) ) {
unset( $fields[['place_of_purchase']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['color_item'] ) ) {
unset( $fields[['color_item']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['product_model'] ) ) {
unset( $fields[['product_model']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['lightbulb_out'] ) ) {
unset( $fields[['lightbulb_out']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['not_turn_on'] ) ) {
unset( $fields[['not_turn_on']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['fan_not_running'] ) ) {
unset( $fields[['fan_not_running']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['strange_noise'] ) ) {
unset( $fields[['strange_noise']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['not_catching'] ) ) {
unset( $fields[['not_catching']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['csr_other'] ) ) {
unset( $fields[['csr_other']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['case_description'] ) ) {
unset( $fields[['case_description']] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields'
);
woocommerce_checkout_fields
in the first place, so they cannot be removed that way. Then you are echoing them out directly on thewoocommerce_after_order_notes
hook. – helgatheviking