首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅在结帐时禁用特定发货方法的支付网关

仅在结帐时禁用特定发货方法的支付网关
EN

Stack Overflow用户
提问于 2020-10-16 09:47:26
回答 1查看 1.2K关注 0票数 0

我在WooCommerce上遇到了一个问题,我想知道是否还有其他人也经历过这个问题。

我销售的某些产品太易碎,无法用UPS/DHL/FedEx运输。所以我必须通过托盘运送这些产品。为了解决我的问题,我创建了一个“请求报价”的发货方法,允许我的客户选择BACS作为付款方法,请求报价作为发货方法,并提交他们的订单。在我计算了运输成本后,我更新订单(将运输方法更改为N/A),并将状态从“待付”改为“待付”,以便允许客户通过卡片付款。

这就是我遇到问题的地方。我注意到,如果我取消了几个支付网关,如果选择了这些特定的发货方法,即使我从订单中删除了发货方法,“订单支付”端点(网站/my/orders/)中的客户也无法使用这些支付网关。

有办法绕道吗?

这是我正在使用的代码,用于禁用特定发运方法的支付网关。

代码语言:javascript
复制
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;
}

在与一些开发人员协商后,更新了,他们建议每次需要支付网关时都运行此代码,并建议我只在结帐页面上运行此代码段。

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

代码语言:javascript
复制
if ( is_checkout_pay_page() ) {
    // unset Payment Gateways
}

解决了,这是我的尝试,它有效。但不确定我们能否表达得更好:

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-17 16:38:33

代码语言:javascript
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64386789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档