隐藏基于WooCommerce中所选送货方式的付款方式

时间:2019-04-02 14:57:16

标签: php wordpress woocommerce shipping-method payment-method

如果通过将以下代码添加到主题function.php中选择了一种送货方式,我试图隐藏两种付款方式

// Filter payment gatways for different shipping methods
function my_custom_available_payment_gateways( $gateways ) {
    $chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
    if ( in_array( 'flat_rate:7', $chosen_shipping_rates ) ) {
        unset( $gateways['stripe'] );
        unset( $gateways['ppec_paypal'] );
    }
    endif;
    return $gateways;
}
 add_filter( 'woocommerce_available_payment_gateways', 
'my_custom_available_payment_gateways' );

一切正常。除了我在产品页面上遇到此错误。

  

警告:
  in_array()期望参数2为数组,在[theme function.php和行号]中指定为null

1 个答案:

答案 0 :(得分:1)

使用以下内容防止此错误(也已删除endif;

// Filter payment gatways for different shipping methods
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways', 10, 1 );
function my_custom_available_payment_gateways( $gateways ) {
    $chosen_shipping_rates = (array) WC()->session->get( 'chosen_shipping_methods' );

    if ( in_array( 'flat_rate:12', $chosen_shipping_rates ) ) {
        unset( $gateways['stripe'] );
        unset( $gateways['ppec_paypal'] );
    }

    return $gateways;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。