根据 Woocommerce 订单状态禁用特定的付款方式 [英] Disable specific payment methods depending on Woocommerce order status
问题描述
我在网站上进行了两步付款.在经理确认订单后付款.首先,用户选择用于确认"的付款方式(更名为货到付款"),并在收到付款发票后付款.在结帐页面上,我通过js隐藏了paypal.我希望在保持状态时隐藏贝宝.当待付款"状态被禁用确认"(更名为货到付款")时,可以通过paypal付款.
I made a two-step payment on the site. Payment occurs after confirmation of the order by the manager. First, the user selects the payment method "for confirmation"(renamed "cash on delivery") and pay only after receiving the invoice for payment. On the checkout page, I hide paypal via js. I would like paypal to be hidden when on-hold status. When the status of "Pending payment" is disabled "for confirmation"(renamed "cash on delivery") and payment via paypal is available.
推荐答案
2020 年 7 月更新
以下代码将显示隐藏支付网关:
The following code will show hide payment gateways:
- 在结帐页面,它将删除paypal"付款选项(因此您可以删除您的 jQuery 代码)
- 在订单支付"页面,它会:
- On checkout page it will remove "paypal" payment option (So you can remove your jQuery code)
- On Order Pay page it will:
- 保留paypal"仅付款选项,如果订单状态为待处理"(删除所有其他选项)
- 对于除待处理"以外的其他订单状态,Woocommerce 不允许付款...
- Keep "paypal" only payment option if the order status is "pending" (removing all other options)
- For others order statuses than "pending", the payment is not allowed by 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 文件中.经过测试和工作.
Code goes in function.php file of your active child theme (or active theme). Tested and work.
这篇关于根据 Woocommerce 订单状态禁用特定的付款方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!