WooCommerce Show Payment Gateways for Logged In Customers Only

烂漫一生 提交于 2019-12-23 03:25:12

问题


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!



来源:https://stackoverflow.com/questions/13864738/woocommerce-show-payment-gateways-for-logged-in-customers-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!