禁用特定送货方式的支付网关

Posted

技术标签:

【中文标题】禁用特定送货方式的支付网关【英文标题】:Disable Payment Gateway For Specific Shipping Method 【发布时间】:2021-06-07 21:55:15 【问题描述】:

按照Disable Payment Gateway For Specific Shipping Method On Checkout Only 回答我之前的问题,其中:

禁用支付网关('cardgatecreditcard'、'cardgategiropay'、'cardgateideal'、'cardgatesofortbanking') 选择特定运输方式时('flat_rate'、'request_shipping_quote')

但是,最后一部分似乎与 WooCommerce 主页发生冲突:

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;


如何进一步解决此问题?

这是致命错误:

Fatal error: Uncaught Error: Call to a member function get() on null in /home/customer/www/multitrance.com/public_html/wp-content/themes/oceanwp-child/functions.php:304
Stack trace:
#0 /home/customer/www/multitrance.com/public_html/wp-includes/class-wp-hook.php(287): filter_woocommerce_available_payment_gateways(Array)
#1 /home/customer/www/multitrance.com/public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters(Array, Array)
#2 /home/customer/www/multitrance.com/public_html/wp-content/plugins/woocommerce/includes/class-wc-payment-gateways.php(160): apply_filters('woocommerce_ava...', Array)
#3 /home/customer/www/multitrance.com/public_html/wp-content/plugins/woocommerce/packages/woocommerce-admin/src/Features/OnboardingTasks.php(92): WC_Payment_Gateways->get_available_payment_gateways()
#4 /home/customer/www/multitrance.com/public_html/wp-content/plugins/woocommerce/packages/woocommerce-admin/src/Features/OnboardingTasks.php(149): Automattic\WooCommerce\Admin\Features\OnboardingTasks::get_settings()
#5 /home/custo in /home/customer/www/multitrance.com/public_html/wp-content/themes/oceanwp-child/functions.php on line 304

第 304 行:

if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;

【问题讨论】:

【参考方案1】:

您需要将代码限制为仅结帐页面:

add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways' );
function filter_woocommerce_available_payment_gateways( $available_gateways )  
    if( is_checkout() && ! is_wc_endpoint_url() ) 
        $gateways_to_disable = array( 'cardgatecreditcard', 'cardgategiropay', 'cardgateideal', 'cardgatesofortbanking' );
        $shipping_methods    = array( 'flat_rate', 'request_shipping_quote' );
        $chosen_shipping     = WC()->session->get( 'chosen_shipping_methods' )[0];
        $disable_gateways    = false;
    
        // Check if we need to disable gateways
        foreach ( $shipping_methods as $shipping_method ) 
            if ( strpos( $chosen_shipping, $shipping_method ) !== false ) 
                $disable_gateways = true;
            
        
        
        // If so, disable the gateways
        if ( $disable_gateways ) 
            foreach ( $available_gateways as $payment_id => $gateway ) 
                if ( in_array( $payment_id, $gateways_to_disable ) ) 
                    unset( $available_gateways[$payment_id] );
                
            
        
    
    return $available_gateways;

它应该可以在主页上正常工作

【讨论】:

以上是关于禁用特定送货方式的支付网关的主要内容,如果未能解决你的问题,请参考以下文章

php 禁用运行WPML的WooCommerce网站的特定语言版本上的特定支付网关

在 Woocommerce 中为特定支付网关添加自定义费用

添加一个复选框作为禁用支付网关的 WooCommerce 管理产品选项

根据 WooCommerce 购物车中的商品数量和产品类别禁用支付网关

为特定产品启用支付网关 - Magento 2

Woocommerce 中特定支付网关的结帐附加字段