Page 1 of 1
Wordpress: How do I add 'administrator' into this function?
Posted: Tue Sep 19, 2017 6:58 am
by simonmlewis
Code: Select all
/**
* @snippet Enable Payment Gateway for a Specific User Role | WooCommerce
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=273
* @author Rodolfo Melogli
* @compatible WooCommerce 2.4.10
*/
function bbloomer_paypal_enable_manager( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['paypal'] ) && !current_user_can('shop_manager') ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_paypal_enable_manager' );
We are using this function to enable Cash on Delivery payments, but we need it to show for Shop_manager and Administrator.
I Assumed I could do it like this:
Code: Select all
/**
* @snippet Enable Payment Gateway for a Specific User Role | WooCommerce
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=273
* @author Rodolfo Melogli
* @compatible WooCommerce 2.4.10
*/
function bbloomer_paypal_enable_manager( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['paypal'] ) && (!current_user_can('shop_manager') || !current_user_can('administrator') )) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_paypal_enable_manager' );
But it didn't show the additional button at all.
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Tue Sep 19, 2017 9:32 am
by requinix
Think about the logic: if the current user is not shop manager or the current user is not administrator then hide.
That means if they're shop manager then they're not administrator so hide it. And if they're administrator then they're not shop manager so hide it.
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Tue Sep 19, 2017 9:35 am
by simonmlewis
Should it be && rather than || ?
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Tue Sep 19, 2017 9:49 am
by requinix
Let's see what the new logic would be:
If the current user is not shop manager and the current user is not administrator then hide.
Fill in the blanks:
- If they're shop manager then...
- If they're administrator then...
- If they're not shop manager and not administrator then...
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Tue Sep 19, 2017 9:52 am
by simonmlewis
If they Shop Manager then show the button.
If they are administrator then show the button.
If they are not the shop manager and not the administrator, do not show the button.
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Tue Sep 19, 2017 10:15 am
by requinix
Sounds like it works.
You can also invert the logic:
Code: Select all
(!shop manager && !administrator) then hide
->
!((!shop manager && !administrator) then hide)
= !(!shop manager && !administrator) then !hide
= (!!shop manager || !!administrator) then show
= (shop manager || administrator) then show
but the code is easier the original way.
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Tue Sep 19, 2017 10:32 am
by simonmlewis
So this should work and show only for Shop_manager and administrator:
Code: Select all
/**
* @snippet Enable Payment Gateway for a Specific User Role | WooCommerce
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=273
* @author Rodolfo Melogli
* @compatible WooCommerce 2.4.10
*/
function bbloomer_paypal_enable_manager( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['paypal'] ) && !current_user_can('shop_manager') || !current_user_can('administrator')) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_paypal_enable_manager' );
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Tue Sep 19, 2017 10:40 am
by requinix
No, actually, it won't because that stuff I was doing didn't include the isset() check.
Stick with what you first thought to do.
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Tue Sep 19, 2017 10:46 am
by simonmlewis
Sorry ??
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Tue Sep 19, 2017 12:13 pm
by requinix
simonmlewis wrote:Should it be && rather than || ?
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Wed Sep 20, 2017 3:33 am
by simonmlewis
Code: Select all
function bbloomer_paypal_enable_manager( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['paypal'] ) && !current_user_can('shop_manager') && !current_user_can('administrator')) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_paypal_enable_manager' );
If we use this, it shows the "Phone by Cash" (custom option) for everyone.
If I do it like though, it shows it only for administrator.
Code: Select all
function bbloomer_cod_enable_manager( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['cod'] ) && (!current_user_can('administrator')) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_cod_enable_manager' );
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Wed Sep 20, 2017 5:28 am
by Celauran
If I've understood what you're trying to do, the condition you want is probably
Code: Select all
if (isset($available_gateways['paypal']) && !(current_user_can('shop_manager') || current_user_can('administrator'))) {
unset($available_gateways['paypal']);
}
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Wed Sep 20, 2017 8:38 am
by simonmlewis
Amazingly, if I do that, it means the option is available to those not logged in.
Re: Wordpress: How do I add 'administrator' into this functi
Posted: Wed Sep 20, 2017 11:38 am
by requinix
Code: Select all
if ( isset( $available_gateways['cod'] ) && !current_user_can('shop_manager') && !current_user_can('administrator')) ) {
Which is confusing to me because I thought we already figured that out...