WooCommerce Show Payment Gateways for Logged In Cu

2019-06-05 01:30发布

问题:

I am setting up an ecommerce site using Wordpress and WooCommerce. We are using the wordpress member accounts to track customer information, and we need a way for logged in members only to be able to choose to purchase their cart "on credit", meaning no payment is required to place the order. Basically what I have done is hi-jacked the "Check" option (since we don't need it anywhere else) and renamed it "Credit" since it allows for the functionality we need.

However, I need a way for the "Credit" (check) option to only display if the user is logged in. Is there any way I can just "unhook" this option if the user isn't logged in? This seems like something that would be easy to do, but I couldn't find anything about it. Any help is appreciated.

回答1:

The original answer to this question (from BWDesign) no longer works due to changes in WooCommerce (at least from WooCommerce 2.1.7 onwards, possibly before). This does the trick now (tested on 2.1.7 and 2.1.8), add it to e.g. your functions.php file:

add_filter( "woocommerce_available_payment_gateways", "rp_filter_gateways", 9999 );

function rp_filter_gateways($args) {
 if(!is_user_logged_in() && isset($args['cheque'])) {
  unset($args['cheque']);
 }
 return $args;
}


回答2:

I just found the solution. In the class-wc-cheque.php file, the check or "cheque" (crazy brits) option is hooked using add_filter('woocommerce_payment_gateways', 'add_cheque_gateway' );. So the solution was simply to add this code to my functions.php file:

 if(!is_user_logged_in()){
     remove_filter('woocommerce_payment_gateways', 'add_cheque_gateway' );
 }

Hope this helps!