Woocommerce结帐 - payment_method调用不正确

时间:2018-05-29 15:02:21

标签: php methods woocommerce payment shipping

使用Woocommerce,当用户选择特定的送货时,我使用的功能隐藏了付款选项:

  public function custom_available_payment_gateways( $gateways ) {
        $chosen_shipping_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();

        if ( in_array( 'local_pickup:14', $chosen_shipping_rates ) ) :
            unset( $gateways['cod'] );
        elseif ( in_array( 'flat_rate:17', $chosen_shipping_rates ) ) :
            unset( $gateways['bacs'] );
            unset( $gateways['przelewy24'] );
        elseif ( in_array( 'flat_rate:18', $chosen_shipping_rates ) ) :
            unset( $gateways['bacs'] );
            unset( $gateways['przelewy24'] );


        endif;
        return $gateways;
    }

添加评论折扣

消息输入

记下一些东西

粗体 斜体〜strike~ code preformatted>引用 搜索结果

包括: 一切 所有 消息 档 1结果

工作正常,但我收到错误 - payment_method调用错误 - 如何更改此功能才能正确调用?

1 个答案:

答案 0 :(得分:1)

我在代码中进行了非常小的更改,并在我的活动子主题的function.php文件中对其进行了测试。它没有问题:

add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );
function custom_available_payment_gateways( $gateways ) {
    $chosen_sm = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();

    if ( in_array( 'local_pickup:14', $chosen_sm ) )
    {
        if( isset( $gateways['cod'] ) )
            unset( $gateways['cod'] );
    } elseif ( in_array( 'flat_rate:17', $chosen_sm ) || in_array( 'flat_rate:18', $chosen_sm ) )
    {
        if( isset( $gateways['bacs'] ) )
            unset( $gateways['bacs'] );
        if( isset( $gateways['przelewy24'] ) )
            unset( $gateways['przelewy24'] );
    }
    return $gateways;
}

因此,对于插件,钩子部分会略有不同(位于init()函数中):

add_filter( 'woocommerce_available_payment_gateways', array($this, 'custom_available_payment_gateways') );
public function custom_available_payment_gateways( $gateways ) {
    $chosen_sm = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();

    if ( in_array( 'local_pickup:14', $chosen_sm ) )
    {
        if( isset( $gateways['cod'] ) )
            unset( $gateways['cod'] );
    } elseif ( in_array( 'flat_rate:17', $chosen_sm ) || in_array( 'flat_rate:18', $chosen_sm ) )
    {
        if( isset( $gateways['bacs'] ) )
            unset( $gateways['bacs'] );
        if( isset( $gateways['przelewy24'] ) )
            unset( $gateways['przelewy24'] );
    }
    return $gateways;
}

它应该有用。