Reputation: 103
With Woocommerce, I am using a function to hide payment options when user select a specific shipping:
public function custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if ( in_array( 'local_pickup:14', $chosen_shipping_rates ) ) :
unset( $gateways['cod'] );
elseif ( in_array( 'flat_rate:17', $chosen_shipping_rates ) ) :
unset( $gateways['bacs'] );
unset( $gateways['przelewy24'] );
elseif ( in_array( 'flat_rate:18', $chosen_shipping_rates ) ) :
unset( $gateways['bacs'] );
unset( $gateways['przelewy24'] );
endif;
return $gateways;
}
Add CommentCollapse
Message Input
Jot something down
bold italics ~strike~ code
preformatted
>quote
Search Results
Include: everything All Messages Files 1 Result
that is working fine but i am getting an error - payment_method was called incorrectly - how can i change this function to call it correctly?
Upvotes: 1
Views: 489
Reputation: 253748
I have made very small changes in your code and I have tested it in the function.php file of my active child theme. It works without problems:
add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );
function custom_available_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
$chosen_sm = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if ( in_array( 'local_pickup:14', $chosen_sm ) )
{
if( isset( $available_gateways['cod'] ) )
unset( $available_gateways['cod'] );
} elseif ( in_array( 'flat_rate:17', $chosen_sm ) || in_array( 'flat_rate:18', $chosen_sm ) )
{
if( isset( $available_gateways['bacs'] ) )
unset( $available_gateways['bacs'] );
if( isset( $available_gateways['przelewy24'] ) )
unset( $available_gateways['przelewy24'] );
}
return $available_gateways;
}
So for a plugin the hook part will be slightly different (and located in the init()
function):
add_filter( 'woocommerce_available_payment_gateways', array($this, 'custom_available_payment_gateways') );
public function custom_available_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
$chosen_sm = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if ( in_array( 'local_pickup:14', $chosen_sm ) )
{
if( isset( $available_gateways['cod'] ) )
unset( $available_gateways['cod'] );
} elseif ( in_array( 'flat_rate:17', $chosen_sm ) || in_array( 'flat_rate:18', $chosen_sm ) )
{
if( isset( $available_gateways['bacs'] ) )
unset( $available_gateways['bacs'] );
if( isset( $available_gateways['przelewy24'] ) )
unset( $available_gateways['przelewy24'] );
}
return $available_gateways;
}
It should work.
Upvotes: 1