Php 禁用本地交货装运方式的BACS付款方式

Php 禁用本地交货装运方式的BACS付款方式,php,wordpress,woocommerce,cart,payment-method,Php,Wordpress,Woocommerce,Cart,Payment Method,如何禁用本地配送方式的BACS付款方式 我已经在functions.php文件中包含了下面的代码,但它不起作用。 也许有人能帮我 function my_custom_available_payment_gateways( $gateways ) { $chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' ); // When 'local delivery' has been ch

如何禁用本地配送方式的BACS付款方式

我已经在functions.php文件中包含了下面的代码,但它不起作用。 也许有人能帮我

function my_custom_available_payment_gateways( $gateways ) {
    $chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
    // When 'local delivery' has been chosen as shipping rate
    if ( in_array( 'local_delivery', $chosen_shipping_rates ) ) :
        // Remove bank transfer payment gateway
        unset( $gateways['bacs'] );
    endif;
    return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );

你不远。要使代码正常工作,您需要操作所选发送方法数组中的数据,以便仅获取foreach循环中的slug

代码如下:

add_filter( 'woocommerce_available_payment_gateways', 'unset_bacs_for_local_delivery' );

function unset_bacs_for_local_delivery( $gateways ) {
    // Not in backend (admin)
    if( is_admin() ) 
        return $gateways;

    // Initialising variables
    $chosen_shipping_method_ids = array();
    $chosen_shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' );

    // Iterating and manipulating the "chosen shipping methods" to get the SLUG
    foreach( $chosen_hipping_methods as $shipping_method_rate_id ) :
         $shipping_method_array = explode(':', $shipping_method_rate_id);
         $chosen_shipping_method_ids[] = $shipping_method_array[0];
    endforeach;

    //When 'local delivery' has been chosen as shipping method
    if ( in_array( 'local_delivery', $chosen_shipping_method_ids ) ) :
        // Remove bank transfer payment gateway
        unset( $gateways['bacs'] );
    endif;

    return $gateways;
}
此代码经过测试,功能齐全

代码进入活动子主题或主题的functions.php文件。或者在任何插件php文件中