【问题标题】:Hide payment methods based on selected shipping method in WooCommerce [duplicate]根据 WooCommerce 中选择的运输方式隐藏付款方式 [重复]
【发布时间】:2019-04-02 14:57:15
【问题描述】:

如果通过将以下代码添加到主题 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

【问题讨论】:

  • the code works properly and when the chosen shipping method selected that two payment gateway are not shown.但我在产品页面中也收到了 null 给定错误。

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


【解决方案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( $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;
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

【讨论】:

  • 谢谢。这解决了这个问题。没有更多错误
猜你喜欢
  • 1970-01-01
  • 2014-08-06
  • 2018-10-28
  • 2018-11-09
  • 2016-04-13
  • 1970-01-01
  • 2019-01-16
  • 2018-03-01
  • 2015-12-01
相关资源
最近更新 更多