質問

I am trying to hide some payment options in Woocommerce in case of specific delivery option. I tried to put this to my functions.php but It´s not working and I don´t know why. Can you help me please?

    function payment_gateway_disable_country( $available_gateways, $available_methods )
    {
    global $woocommerce;
    if ( isset( $available_methods['local_delivery'] ) ){
    unset( $available_gateways['paypal'] );
    }
    return $available_gateways;
    }
    add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

My research:

link 1

link 2

link 3

link 4

役に立ちましたか?

解決

The available delivery methods do not get passed as a parameter in the filter woocommerce_available_payment_gateways - you need to load them in and check them.

The code below should remove the paypal payment option one if the user selects local delivery. If your checkout page is the AJAX based one then as the user changes the delivery method, the available payment options should also change.

function payment_gateway_disable_country($available_gateways) {

    global $woocommerce;

    $packages = $woocommerce->shipping->get_packages();

    foreach ( $packages as $i => $package ) {
        $chosen_method = isset( $woocommerce->session->chosen_shipping_methods[ $i ] ) ?
            $woocommerce->session->chosen_shipping_methods[ $i ] :  '';

        if ('local_delivery' == $chosen_method) {  
            unset($available_gateways['paypal']);
            break;
        }
    }

    return $available_gateways;

}

add_filter(
    'woocommerce_available_payment_gateways',
    'payment_gateway_disable_country'
);

Let me know if you have any issues with the code; I did not have a chance to test it with woocommerce.

他のヒント

$available_methods will not be accessible inside your function. So first define it globally & access as global variable inside function, somewhat like this:

global $available_methods;
$available_methods = array( 'local_delivery' => 'yes' );

function payment_gateway_disable_country( $available_gateways )
{
global $woocommerce, $available_methods;
if ( isset( $available_methods['local_delivery'] ) ){
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top