根据 Woocommerce 订单状态禁用特定的付款方式

Disable specific payment methods depending on Woocommerce order status

我在网站上进行了两步支付。经理确认订单后付款。首先,用户选择付款方式"for confirmation"(已更名为"cash on delivery"),收到付款发票后才付款。在结帐页面上,我通过js隐藏了paypal。我希望在暂停状态下隐藏贝宝。当 "Pending payment" 的状态被禁用 "for confirmation"(重命名为 "cash on delivery")并且可以通过 paypal 付款。

2020 年 7 月更新

以下代码将显示隐藏支付网关:

  1. 在结帐页面 它将删除“paypal”付款选项(因此您可以删除您的 jQuery 代码)
  2. 在订单支付页面 它将:
  • 如果订单状态为“待定”(删除所有其他选项),则仅保留“paypal”付款选项
  • 对于其他订单状态不是“待处理”,Woocommerce 不允许付款

代码:

// Show/hide payment gateways
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;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。

复制并粘贴相同的代码但没有成功 语法错误,意外 'elseif' (T_ELSEIF)

我更正代码

// Show/hide payment gateways
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;
}

enter image description here