M
M
macstuff2019-03-02 10:40:20
WooCommerce
macstuff, 2019-03-02 10:40:20

How to disable a payment method if a certain status is not assigned?

Hey! It was necessary to implement a two-stage payment. Renamed the "cash" method to "for confirmation" and began to hide Yandex checkout using js, but this method only works on checkout, when paying, checkout / order-pay is already used and it shows all payment methods and users often confuse and choose again for confirmation .
How to write a hook to disable payment if the status is not "pending payment" and to allow it if "pending payment".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
macstuff, 2019-03-06
@macstuff

Below is an example found on another site. There is one point, an extra PayPal payment method is used, perhaps you can help eliminate it. The method is working and can be used

// 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['cheque']);
                unset($available_gateways['bacs']);
                unset($available_gateways['paypal']);
            }
        }
    }
    // 2. On Checkout page
    elseif( is_checkout() && ! is_wc_endpoint_url() ) {
        // Disable paypal
        if( isset($available_gateways['paypal']) ) {
            unset($available_gateways['modulbank']);
            unset($available_gateways['kassa']);
            unset($available_gateways['paypal']);
        }
    }
    return $available_gateways;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question