Search code examples
phpwordpresswoocommercepayment-gatewaypayment-method

How to get the ID of a payment method in Woocommerce?


Maybe someone knows, how to add a condition: if the payment amount is less than 3000 - certain payment method is hidden?

For example, there are 2 payment methods:

  • cash
  • online payment

If the amount is less than 3000, the "cash" method is hidden.

As far as I understand, I need to get the payment gateway ID, and then apply the snippet:

add_filter( 'woocommerce_available_payment_gateways', 'custom_paypal_disable_manager' );
function custom_paypal_disable_manager( $available_gateways ) {
   if ( $total_amount < 3000 ) {
      unset( $available_gateways['ID payment gateway'] );
   return $available_gateways;
}

But I don't know how to get the payment gateway ID (there are several payment methods and they are all implemented by different plugins). Perhaps there is a way to get all IDs of payment gateways in a list.

I would be grateful for any information.


Solution

  • Get the payment method ID

    Using the following code, will display near the footer on front end, the payment ID visible only to administrator and shop manage user roles:

    add_action( 'wp_footer', 'display_payment_method_ids_for_admins' );
    function display_payment_method_ids_for_admins(){
        if( current_user_can( 'administrator') || current_user_can( 'shop_manager') ) {
            $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
    
            if ( ! $available_gateways ) {
                return;
            }
            $output_html = '<table style="max-width:480px;border:solid 1px #ccc;">
            <thead><tr><td>Payment Title</td><td>Payment ID</td></tr></thead>
            <tbody>';
    
            foreach( $available_gateways as $payment_id => $gateway ) {
                $output_html .= '<tr><td>'.$gateway->get_title().'</td><td><code>'.$payment_id.'</code></td></tr>';
            }
            echo '<pre>'.$output_html.'</tbody></table></pre>';
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme).
    Once used, remove it.

    enter image description here