gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:58:59 24 4
gpt4 key购买 nike

关注 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 :
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;

最佳答案

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

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 - 禁用特定送货方式的支付网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66556570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com