-1

如何禁用送货方式BACS的付款方式?local delivery

我已将以下代码包含到我的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' );
4

1 回答 1

2

你不远了。要使您的代码正常工作,您需要操作数组中的数据chosen shipping methods以仅获取 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 文件中。

于 2016-12-13T03:44:22.437 回答