skip to Main Content

Please,

how can i set on Woocommerce the payment method "payment on delivery" only for Admin Profile User?

Is that possible?

This means, the payment method "payment on delivery" will be available on the checkout page only for users logged on WP and with admin profile.

Gratitude for the help!

2

Answers


  1. yes with woocommerce_available_payment_gateways and one condition is_user_admin()

    Example

    // define the woocommerce_available_payment_gateways callback 
    function filter_woocommerce_available_payment_gateways( $available_gateways ) { 
        $delete = false;
        foreach ($available_gateways as $key => $gateway) {
            if($gateway === [[[[payment on delivery]]]] && !is_user_admin()){
                unset($available_gateways[$key]);
                break;
            }
        }
        return $available_gateways; 
    }; 
             
    // add the filter 
    add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
    
    Login or Signup to reply.
  2. I was able to get it working by adjusting Sco’s code to the following:

        // remove certain payment options if not logged in as staff
        public function checkout_limit_payment_methods($available_gateways){
        $delete = false;
        foreach ($available_gateways as $key => $gateway) {
            if($key === "bacs" || $key === "cheque" && !is_user_admin()){
                        if ( is_user_logged_in() ) {
                            $user = wp_get_current_user();
                            $allowed_roles = array( 'shop_manager', 'editor', 'administrator', 'author' );
                            if ( !array_intersect( $allowed_roles, $user->roles ) ) {
                            // delete payment methods for any user without roles in array
                    unset($available_gateways[$key]);
                            }
                        } else {
                        // delete payment methods for any user not logged in
                unset($available_gateways[$key]);
                }
                    }
        }
        return $available_gateways; 
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search