有条件地隐藏和显示支付网关
Conditionally hiding et showing payment gateways
在 Woocommerce 中,我想在订单前的 "checkout" 页面上隐藏 "paypal" 网关是第一次创建,只显示 "cash on delivery" 网关(标记为 Reserve)。
另一方面,在checkout/order-pay页面当订单状态为"pending"时,隐藏'Reserve' 网关并显示 "paypal"。 (当我们手动将订单状态更改为 "pending" 并将发票连同付款 link 一起发送给客户时,就会发生这种情况。
我认为应该通过检查订单状态和使用 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' );
我也试过 WC()->query_vars['order']
而不是 wc_get_order();
到 并检查它的状态,但它也没有用。
我saw woocommerce_order_items_table
action hook但是也获取不到订单
如何在 checkout/order-pay 页面上检索 ID 和订单状态?
2021 年更新
如果我没理解错的话,您想要 set/unset 您可用的支付网关,具体取决于实时生成的订单,其状态必须为待处理才能拥有“paypal”网关。 Ian 在所有其他情况下,可用网关仅为“保留”(重命名为“cod”支付网关)。
此代码使用 get_query_var()
检索实时订单 ID,这样:
add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );
function custom_available_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
if ( is_wc_endpoint_url( 'order-pay' ) ) {
$order = wc_get_order( absint( get_query_var('order-pay') ) );
if ( is_a( $order, 'WC_Order' ) && $order->has_status('pending') ) {
unset( $available_gateways['cod'] );
} else {
unset( $available_gateways['paypal'] );
}
} else {
unset( $gateways['paypal'] );
}
return $available_gateways;
}
代码进入您的活动子主题(或主题)的 functions.php 文件或任何插件文件。
代码已经过测试并且有效。
在 Woocommerce 中,我想在订单前的 "checkout" 页面上隐藏 "paypal" 网关是第一次创建,只显示 "cash on delivery" 网关(标记为 Reserve)。
另一方面,在checkout/order-pay页面当订单状态为"pending"时,隐藏'Reserve' 网关并显示 "paypal"。 (当我们手动将订单状态更改为 "pending" 并将发票连同付款 link 一起发送给客户时,就会发生这种情况。
我认为应该通过检查订单状态和使用 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' );
我也试过 WC()->query_vars['order']
而不是 wc_get_order();
到
我saw woocommerce_order_items_table
action hook但是也获取不到订单
如何在 checkout/order-pay 页面上检索 ID 和订单状态?
2021 年更新
如果我没理解错的话,您想要 set/unset 您可用的支付网关,具体取决于实时生成的订单,其状态必须为待处理才能拥有“paypal”网关。 Ian 在所有其他情况下,可用网关仅为“保留”(重命名为“cod”支付网关)。
此代码使用 get_query_var()
检索实时订单 ID,这样:
add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );
function custom_available_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
if ( is_wc_endpoint_url( 'order-pay' ) ) {
$order = wc_get_order( absint( get_query_var('order-pay') ) );
if ( is_a( $order, 'WC_Order' ) && $order->has_status('pending') ) {
unset( $available_gateways['cod'] );
} else {
unset( $available_gateways['paypal'] );
}
} else {
unset( $gateways['paypal'] );
}
return $available_gateways;
}
代码进入您的活动子主题(或主题)的 functions.php 文件或任何插件文件。
代码已经过测试并且有效。