【问题标题】:Disable Payment Gateway For Specific Shipping Method On Checkout Only仅在结帐时禁用特定运输方式的支付网关
【发布时间】:2021-01-30 20:03:52
【问题描述】:

我在 WooCommerce 中遇到了一个问题,我想知道是否其他人也遇到过。

我销售的某些产品太脆弱而无法通过 UPS/DHL/FedEx 运送。所以我必须通过托盘运送这些产品。为了解决我的问题,我创建了一个“请求报价”运输方式,允许我的客户选择 BACS 作为付款方式,请求报价作为运输方式并提交他们的订单。在我计算好运费后,我更新订单(将运输方式更改为 N/A)并将状态从“暂停”更改为“待付款”,以便客户可以通过卡付款,如果他们愿意.

这就是我遇到问题的地方。我注意到,如果我取消设置多个支付网关并且选择了这些特定的运输方式,即使我删除了这些支付网关,客户也无法在“order-pay”端点(网站/我的帐户/订单/)中使用这些支付网关订单中的送货方式。

有没有办法解决这个问题?

这是我用来禁用特定送货方式的支付网关的代码。

add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );

function filter_woocommerce_available_payment_gateways( $available_gateways ) { 

    $gateways_to_disable = array( 'cardgatecreditcard', 'cardgategiropay', 'cardgateideal', 'cardgatesofortbanking' );
    $shipping_methods = array( 'flat_rate', 'request_shipping_quote' );
    $disable_gateways = false;

    // Check if we need to disable gateways
    foreach ( $shipping_methods as $shipping_method ) {
        if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
    }
    
    // If so, disable the gateways
    if ( $disable_gateways ) {
        foreach ( $available_gateways as $id => $gateway ) {
            if ( in_array( $id, $gateways_to_disable ) ) {
                unset( $available_gateways[$id] );
            }
        }
    }
    return $available_gateways;
}

更新在咨询了一些开发人员后,他们建议每次需要支付网关时都会运行此代码,并建议我仅在结帐页面上运行此 sn-p。

他们建议在我的代码中添加以下内容:

if ( is_checkout_pay_page() ) {
    // unset Payment Gateways
}

已解决这是我的尝试,它有效。但不确定我们是否可以更好地表达:

add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );

function filter_woocommerce_available_payment_gateways( $available_gateways ) { 

    if ( ! ( is_checkout_pay_page() ) ) {

    $gateways_to_disable = array( 'cardgatecreditcard', 'cardgategiropay', 'cardgateideal', 'cardgatesofortbanking' );
    $shipping_methods = array( 'flat_rate', 'request_shipping_quote' );
    $disable_gateways = false;

    // Check if we need to disable gateways
    foreach ( $shipping_methods as $shipping_method ) {
        if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
    }
    
    // If so, disable the gateways
    if ( $disable_gateways ) {
        foreach ( $available_gateways as $id => $gateway ) {
            if ( in_array( $id, $gateways_to_disable ) ) {
                unset( $available_gateways[$id] );
            }
        }
    }
    return $available_gateways;
}
else { return $available_gateways;
    }
}

【问题讨论】:

    标签: php wordpress woocommerce orders payment-method


    【解决方案1】:
    add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
    
    function filter_woocommerce_available_payment_gateways( $available_gateways ) { 
    
        if ( ! ( is_checkout_pay_page() ) ) {
    
        $gateways_to_disable = array( 'paymentgateway1', 'paymentgateway2', 'paymentgateway3' );
        $shipping_methods = array( 'shippingmethod1', 'shippingmethod2', 'shippingmethod3' );
        $disable_gateways = false;
    
        // Check if we need to disable gateways
        foreach ( $shipping_methods as $shipping_method ) {
            if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
        }
        
        // If so, disable the gateways
        if ( $disable_gateways ) {
            foreach ( $available_gateways as $id => $gateway ) {
                if ( in_array( $id, $gateways_to_disable ) ) {
                    unset( $available_gateways[$id] );
                }
            }
        }
        return $available_gateways;
    }
    else { return $available_gateways;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 2012-12-28
      • 2019-06-15
      • 2021-07-08
      • 1970-01-01
      相关资源
      最近更新 更多