1

By default the store only accepts credit card but I need to allow some pre-approved customers to have the ability to pay by check.

I got this working with a custom user role and the following code:

add_filter( 'woocommerce_available_payment_gateways', 'allow_to_pay_by_check' );

function allow_to_pay_by_check( $available_gateways ) {
   if ( isset( $available_gateways['cheque'] ) && ! current_user_can('pay_using_cheque') ) {
      unset( $available_gateways['cheque'] );
   } 
   return $available_gateways;
}

It works and gives them the ability to pay by check AND credit cards. The issue is that I don't think this should be a user role. It should be located under each customer (Users) account details as a check box to turn on or off. Is this possible?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
user1822824
  • 2,478
  • 6
  • 41
  • 65

1 Answers1

1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks! This works but there's a small issue that is the same as my previous code. Check payment should now be the default selection but it is not. Credit card is selected even though I made check payments #1 under payment methods. Is there anyway to make check payments selected by default when a user has this enabled? – user1822824 Aug 15 '20 at 10:34
  • @user1822824 This is what you asked: *"The issue is that I don't think this should be a user role. It should be located under each customer (Users) account details as a check box to turn on or off. Is this possible?"*. So my answer code answers your initial question: It adds a checkbox option to admin user pages to allow payment by cheque (disallowed by default)… You didn't ask anything about default payment selection in your initial question. Please note that the rule on StackOverFlow is one question at the time. – LoicTheAztec Aug 15 '20 at 12:20