禁用本地送货方式的

时间:2016-12-12 18:11:19

标签: php wordpress woocommerce cart payment-method

如何为 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' );

1 个答案:

答案 0 :(得分:2)

你不远。要使代码正常工作,您需要操作 chosen shipping methods 数组中的数据,以便只获取foreach循环中的slu。。

以下是代码:

add_filter( 'woocommerce_available_payment_gateways', 'unset_bacs_for_local_delivery' );

function unset_bacs_for_local_delivery( $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;
}

此代码经过测试且功能齐全。

代码进入活动子主题(或主题)的function.php文件。或者也可以在任何插件php文件中。