Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在WooCommerce中隐藏基于所选装运方式的付款方式_Php_Wordpress_Woocommerce_Shipping Method_Payment Method - Fatal编程技术网

Php 在WooCommerce中隐藏基于所选装运方式的付款方式

Php 在WooCommerce中隐藏基于所选装运方式的付款方式,php,wordpress,woocommerce,shipping-method,payment-method,Php,Wordpress,Woocommerce,Shipping Method,Payment Method,我试图通过在theme function.php中添加下面的代码来隐藏两种付款方式,如果选择了一种发货方式 // Filter payment gatways for different shipping methods function my_custom_available_payment_gateways( $gateways ) { $chosen_shipping_rates = WC()->session->get( 'chosen_shipping_method

我试图通过在theme 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


使用以下方法防止此错误(也已删除
endif;
):


代码进入活动子主题(或活动主题)的functions.php文件。它应该可以正常工作。

代码正常工作,并且当选择的发货方式时,两个支付网关不会显示。但是我在产品页面中也得到了null给定错误。谢谢。这就解决了问题。不再有错误了
// 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( $available_gateways ) {
if( is_admin() ) return $available_gateways; // Only for frontend

    $chosen_shipping_rates = (array) WC()->session->get( 'chosen_shipping_methods' );

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

    return $available_gateways;
}