首页 > 解决方案 > 根据 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;
}

在此处输入图像描述

标签: wordpresswoocommerce

解决方案


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;
}

推荐阅读