• Resolved haidermaa

    (@haidermaa)


    Hello!

    I would like to know how to disable Cash on Delivery (COD) Payment Method for some users within Customers User Role.

    What i mean is not to disable a COD Payment Method for particular user role but on a particular customer himself.

    Looking forward to your reply!

    Sincerely,

    Hayder

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );
    function custom_available_payment_gateways( $gateways ) {
      $current_user_id = get_current_user_id();
      $bad_lad_id = 1; // user id for the customer who can't have COD
      if ( $bad_lad_id == $current_user_id ) {
        unset( $gateways['cod'] );
      }
      return $gateways;
    }
    Thread Starter haidermaa

    (@haidermaa)

    That worked perfectly! Thank you so much!

    But lets say i have multiple users whom i need to disable COD payment method for them. How can i do that?

    The simplist way would be to repeat the if sequence:

    if ( 7 == $current_user_id ) {
      unset( $gateways['cod'] );
    }
    if ( 42 == $current_user_id ) {
      unset( $gateways['cod'] );
    }
    if ( 80 == $current_user_id ) {
      unset( $gateways['cod'] );
    }
    Thread Starter haidermaa

    (@haidermaa)

    So the complete code would be like this?

    add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );
    function custom_available_payment_gateways( $gateways ) {
      $current_user_id = get_current_user_id();
      $bad_lad_id = 1; // user id for the customer who can't have COD
      if ( $bad_lad_id == $current_user_id ) {
        unset( $gateways['cod'] );
      }
    if ( 7 == $current_user_id ) {
      unset( $gateways['cod'] );
     }
    if ( 42 == $current_user_id ) {
      unset( $gateways['cod'] );
    }
    if ( 80 == $current_user_id ) {
      unset( $gateways['cod'] );
    }
    
      return $gateways;
    }

    Correct?

    • This reply was modified 3 years, 7 months ago by haidermaa.
    • This reply was modified 3 years, 7 months ago by haidermaa.

    You have an extra back tick in there, so:

    add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );
    function custom_available_payment_gateways( $gateways ) {
      $current_user_id = get_current_user_id();
      if ( 1 == $current_user_id ) {
        unset( $gateways['cod'] );
      }
      if ( 7 == $current_user_id ) {
        unset( $gateways['cod'] );
      }
      if ( 42 == $current_user_id ) {
        unset( $gateways['cod'] );
      }
      if ( 80 == $current_user_id ) {
        unset( $gateways['cod'] );
      }
    }

    The backticks are just for formatting on this forum, you wouldn’t have them in your code.

    The code goes in functions.php for your child theme, or you can try a snippets plugin.

    Thread Starter haidermaa

    (@haidermaa)

    Okay, i’m trying to disable one user with your code but i’m seeing this upon testing it.

    https://ibb.co/3vyLyGj

    This is code i used below.

    add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );
    function custom_available_payment_gateways( $gateways ) {
      $current_user_id = get_current_user_id();
      if ( 24 == $current_user_id ) {
        unset( $gateways['cod'] );
      }
    }

    Am i doing it right?

    Sorry forgot return $gateways; on my second code, so:

    function custom_available_payment_gateways( $gateways ) {
      $current_user_id = get_current_user_id();
      if ( 24 == $current_user_id ) {
        unset( $gateways['cod'] );
      }
      if ( 48 == $current_user_id ) {
        unset( $gateways['cod'] );
      }
      return $gateways;
    }
    Thread Starter haidermaa

    (@haidermaa)

    Worked Perfectly!

    Thank you so much!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to disable COD Payment Method on a Particular User?’ is closed to new replies.