【问题标题】:Disabling BACS payment method for local delivery shipping method禁用本地配送方式的 BACS 付款方式
【发布时间】:2016-12-12 18:11:19
【问题描述】:

如何禁用 local delivery 送货方式的 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' );

【问题讨论】:

    标签: php wordpress woocommerce cart payment-method


    【解决方案1】:

    你不远。要使您的代码正常工作,您需要操作 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 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-25
      • 2013-05-03
      • 2012-03-01
      • 2018-03-08
      • 1970-01-01
      • 2018-10-28
      • 2015-05-31
      • 2015-08-31
      相关资源
      最近更新 更多