The following will add to admin users pages a custom checkbox field that will enable or disable "Cheque" payment method:
// Add allowed custom user field in admin
add_action( 'show_user_profile', 'add_customer_checkbox_field', 10 );
add_action( 'edit_user_profile', 'add_customer_checkbox_field', 10 );
function add_customer_checkbox_field( $user )
{
?>
<h3><?php _e("Payment option"); ?></h3>
<table class="form-table">
<tr>
<th><?php _e("Pay by Cheque"); ?></th>
<td>
<?php
woocommerce_form_field( 'pay_by_cheque', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Allowed'),
), get_user_meta( $user->id, 'pay_by_cheque', true ) );
?>
</td>
</tr>
</table>
<?php
}
// Save allowed custom user field in admin
add_action( 'personal_options_update', 'save_customer_checkbox_field' );
add_action( 'edit_user_profile_update', 'save_customer_checkbox_field' );
function save_customer_checkbox_field( $user_id )
{
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta( $user_id, 'pay_by_cheque', isset($_POST['pay_by_cheque']) ? '1' : '0' );
}
}
// Enabling or disabling "Cheque" payment method
add_filter( 'woocommerce_available_payment_gateways', 'allow_to_pay_by_cheque' );
function allow_to_pay_by_cheque( $available_gateways ) {
if ( isset( $available_gateways['cheque'] ) && ! get_user_meta( get_current_user_id(), 'pay_by_cheque', true ) ) {
unset( $available_gateways['cheque'] );
}
return $available_gateways;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.