wordpress - 根据 Woocommerce 订单状态语法错误禁用特定付款方式,意外的“elseif”(T_ELSEIF)
问题描述
帮我解决这个错误
我正在执行相同的功能根据 Woocommerce 订单状态禁用特定的付款方式我得到复制并粘贴相同的代码,但它不起作用语法错误,意外的“elseif”(T_ELSEIF)
add_filter( 'woocommerce_available_payment_gateways', 'conditionally_hide_payment_gateways', 100, 1 );
function conditionally_hide_payment_gateways( $available_gateways ) {
// 1. On Order Pay page
if( is_wc_endpoint_url( 'order-pay' ) ) {
// Get an instance of the WC_Order Object
$order = wc_get_order( get_query_var('order-pay') );
// Loop through payment gateways 'pending', 'on-hold', 'processing'
foreach( $available_gateways as $gateways_id => $gateways ){
// Keep paypal only for "pending" order status
elseif ($gateways_id !== 'paypal' && $order->has_status('pending') ) {
unset($available_gateways[$gateways_id]);
}
}
}
// 2. On Checkout page
elseif( is_checkout() && ! is_wc_endpoint_url() ) {
// Disable paypal
if( isset($available_gateways['paypal']) ) {
unset($available_gateways['paypal']);
}
}
return $available_gateways;
}
解决方案
foreach 中的 elseif 没有前面的 if,所以这是你的错误。
add_filter( 'woocommerce_available_payment_gateways', 'conditionally_hide_payment_gateways', 100, 1 );
function conditionally_hide_payment_gateways( $available_gateways ) {
// 1. On Order Pay page
if( is_wc_endpoint_url( 'order-pay' ) ) {
// Get an instance of the WC_Order Object
$order = wc_get_order( get_query_var('order-pay') );
// Loop through payment gateways 'pending', 'on-hold', 'processing'
foreach( $available_gateways as $gateways_id => $gateways ){
// Keep paypal only for "pending" order status
if ($gateways_id !== 'paypal' && $order->has_status('pending') ) {
unset($available_gateways[$gateways_id]);
}
}
}
// 2. On Checkout page
elseif( is_checkout() && ! is_wc_endpoint_url() ) {
// Disable paypal
if( isset($available_gateways['paypal']) ) {
unset($available_gateways['paypal']);
}
}
return $available_gateways;
}
推荐阅读
- javascript - (未经授权)从 localhost 请求时 - “node testRequest.js”时没有错误
- django - 如果 Django 模型中的函数调用包含 db 查询而其他函数工作正常,为什么它不刷新值?
- java - 如何使用 Spring-Boot 清除 @Async 任务的所有已完成的 Future 结果?
- python - bigquery python中的流式插入
- javascript - jQuery将类添加到包含具有类的元素的div
- java - 如果我安装一个新库,我的应用程序仍然是可移植的吗?
- javascript - 如何在three.js中操作截锥体
- reactjs - React Apollo 默认 fetchPolicy
- c# - 将数据从数据库表传递到数组
- angularjs - AngularJS、Typescript 和 Oclazyload 无法识别范围