问题
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.
回答1:
Update July 2020
The following code will show hide payment gateways:
- On checkout page it will remove "paypal" payment option (So you can remove your jQuery code)
- On Order Pay page it will:
- 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…
The code:
// 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;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
回答2:
Copy and paste the same code and it didn't work syntax error, unexpected 'elseif' (T_ELSEIF)
I correct the code
// 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
来源:https://stackoverflow.com/questions/54989047/disable-specific-payment-methods-depending-on-woocommerce-order-status