2

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.

LoicTheAztec
  • 207,510
  • 22
  • 296
  • 340
  • This is really not clear: *"When the status of "Pending payment" is disabled "for confirmation"(renamed "cash on delivery") and payment via paypal is available"*… Could you reword it please. – LoicTheAztec Mar 04 '19 at 21:42

2 Answers2

4

Update July 2020

The following code will show hide payment gateways:

  1. On checkout page it will remove "paypal" payment option (So you can remove your jQuery code)
  2. 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.

LoicTheAztec
  • 207,510
  • 22
  • 296
  • 340
  • Thank you!!! Thank you very much! I did not immediately understand why a white screen appeared, but after carefully looking, I saw that the status was not correct. Change the status from "peding" to "pending" for others please. And may be you know, how to make a non-selected payment method? – Aleksey Averin Mar 05 '19 at 20:49
  • Where can I find available_gateways names? For example I have bank transfer and Stripe as active payment methods, and I want to have only bank transfer at checkout and also on order page. – mrRobot Apr 21 '21 at 17:30
  • @mrRobot See [this answer thread *(at the end)*](https://stackoverflow.com/a/38384483/3730754) and If you like/want you could please [upvote](https://stackoverflow.com/help/someone-answers) the linked answer. – LoicTheAztec Apr 21 '21 at 17:44
  • How would I be able to remove more than one payment option (besides just PayPal) on the checkout page and then show them all on the order-pay page. I'm trying to do something similar, with Order Proposals, so customers checking out will only be able to see "Order Proposal" as a payment gateway to checkout and when their payment is manually set to pending payment, they will see all available payment gateways, EXCEPT Order Proposal. – David Sep 03 '21 at 22:19
0

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