有条件地隐藏和显示支付网关

Dum*_*ner 3 php wordpress checkout orders woocommerce

在 Woocommerce 中,我想在第一次创建订单之前在“结账”页面上隐藏“paypal”网关,只显示“货到付款”网关(标记为Reserve)。

另一方面,在结帐/订单支付页面上,当订单状态为“待处理”时,隐藏“预订”网关并显示“paypal”。(当我们手动将订单状态更改为“待处理”并将带有付款链接的发票发送给客户时,就会发生这种情况)。

我认为应该通过检查订单状态并使用woocommerce_available_payment_gateways过滤器挂钩来完成。但我在获取当前订单状态时遇到问题。

另外,我不确定用户在结账页面上新创建的订单的状态是什么,但该订单仍然没有显示在管理后端中。

这是我不完整的代码:

function myFunction( $available_gateways ) {

    // How to check if the order's status is not pending payment?
    // How to pass the id of the current order to wc_get_order()?
     $order = wc_get_order($order_id); 

    if ( isset($available_gateways['cod']) && /* pending order status?? */ ) { 
        // hide "cod" gateway
    } else {
        // hide "paypal" gateway
    }
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'myFunction' );
Run Code Online (Sandbox Code Playgroud)

我也尝试获取WC()->query_vars['order']当前订单并检查其状态,但它也不起作用。wc_get_order();

看到了 woocommerce_order_items_table动作挂钩,但也无法得到订单。

如何在结帐/订单支付页面上检索订单 ID 和状态?

Loi*_*tec 6

2021 年更新

\n

如果我理解正确,您想要设置/取消设置可用的支付网关,具体取决于实时生成的订单,其状态必须等待才能拥有“paypal”网关。在所有其他情况下,可用网关仅为“保留”(重命名为“cod”支付网关)。

\n

此代码使用 检索实时订单 ID get_query_var(),如下所示:

\n
add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );\nfunction custom_available_payment_gateways( $available_gateways ) {\n    // Not in backend (admin)\n    if( is_admin() ) \n        return $available_gateways;\n\n    if ( is_wc_endpoint_url( 'order-pay' ) ) {\n        $order = wc_get_order( absint( get_query_var('order-pay') ) );\n\n        if ( is_a( $order, 'WC_Order' ) && $order->has_status('pending') ) {\n            unset( $available_gateways['cod'] );\n        }\xc2\xa0else {\n            unset( $available_gateways['paypal'] );\n        }\n    } else {\n        unset( $gateways['paypal'] );\n    }\n    return $available_gateways;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

代码位于活动子主题(或主题)的functions.php 文件中或任何插件文件中。

\n

该代码经过测试并且可以工作。

\n