PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Tue Sep 19, 2017 6:58 am
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Sep 19, 2017 9:32 am
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.
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Tue Sep 19, 2017 9:35 am
Should it be && rather than || ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Sep 19, 2017 9:49 am
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...
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Tue Sep 19, 2017 9:52 am
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Sep 19, 2017 10:15 am
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.
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Tue Sep 19, 2017 10:32 am
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' );
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Sep 19, 2017 10:40 am
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.
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Tue Sep 19, 2017 10:46 am
Sorry ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Sep 19, 2017 12:13 pm
simonmlewis wrote: Should it be && rather than || ?
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Wed Sep 20, 2017 3:33 am
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' );
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Wed Sep 20, 2017 5:28 am
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']);
}
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Wed Sep 20, 2017 8:38 am
Amazingly, if I do that, it means the option is available to those not logged in.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Sep 20, 2017 11:38 am
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...