PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Dec 16, 2018 8:46 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Tue Sep 19, 2017 6:58 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4426
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
/**
 * @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:

Syntax: [ Download ] [ Hide ]
/**
 * @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.


Top
 Profile  
 
PostPosted: Tue Sep 19, 2017 9:32 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Tue Sep 19, 2017 9:35 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4426
Location: United Kingdom
Should it be && rather than || ?

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Tue Sep 19, 2017 9:49 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Let's see what the new logic would be:
Quote:
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...


Top
 Profile  
 
PostPosted: Tue Sep 19, 2017 9:52 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4426
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Tue Sep 19, 2017 10:15 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Sounds like it works.

You can also invert the logic:
Code:
(!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.


Top
 Profile  
 
PostPosted: Tue Sep 19, 2017 10:32 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4426
Location: United Kingdom
So this should work and show only for Shop_manager and administrator:
Syntax: [ Download ] [ Hide ]
/**
 * @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.


Top
 Profile  
 
PostPosted: Tue Sep 19, 2017 10:40 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Tue Sep 19, 2017 10:46 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4426
Location: United Kingdom
Sorry ??

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Tue Sep 19, 2017 12:13 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
simonmlewis wrote:
Should it be && rather than || ?


Top
 Profile  
 
PostPosted: Wed Sep 20, 2017 3:33 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4426
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
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.
Syntax: [ Download ] [ Hide ]
 
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.


Top
 Profile  
 
PostPosted: Wed Sep 20, 2017 5:28 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6424
Location: Montreal, Canada
If I've understood what you're trying to do, the condition you want is probably
Syntax: [ Download ] [ Hide ]
if (isset($available_gateways['paypal']) && !(current_user_can('shop_manager') || current_user_can('administrator'))) {
    unset($available_gateways['paypal']);
}

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 20, 2017 8:38 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4426
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Wed Sep 20, 2017 11:38 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Syntax: [ Download ] [ Hide ]
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...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Exabot [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group