How to get the id for a Payment Method in WooCommerce

If you have access to an order object in WooCommerce then it is possible to call the get_payment_method()function to see what payment method was used for the order. You could use use the call in an if statement to run some logic if a particular payment method was used. Here’s some code to illustrate what I’m talking about

$order_id = 16;

$order = wc_get_order( $order_id );

if( 'cod' == $order->get_payment_method() ) {
    //run some logic here if the order was paid using the cash on delivery method
}

But how do you know what payment method the value “cod” refers to? There are a couple of methods you could use to find out the answer to this question. Let’s look at them both now.

Via the Admin Interface

If you go to the admin area of the WooCommerce site you are working on  and choose WooCommerce -> Settings from the left hand menu then the “Payments” tab you’ll see a screen that lists all of the payment gateways set up for the store.

If you hover over any of the links you can see the id of the payment method show in the link that appears towards the bottom left of the screen, the id is the text that follows “section=”.

The image below shows that the id “cod” is linked to the “Cash on delivery” payment method

view the code of a payment method in the admin area

Via Code

You can also use code to list all of the payment methods and their ids, here’s the code that will output the details to the screen

echo "<h2>Payment Gateways</h2>";
foreach ( WC()->payment_gateways->payment_gateways() as $payment_gateway_id => $payment_gateway_item ) {
    echo "Payment gateway '" . $payment_gateway_item->title . "' has id '" .$payment_gateway_id . "'<br>";
}

echo "<h2>Available Payment Gateways</h2>";
foreach ( WC()->payment_gateways->get_available_payment_gateways() as $payment_gateway_id => $payment_gateway_item ) {
    echo "Payment gateway '" . $payment_gateway_item->title . "' has id '" .$payment_gateway_id . "'<br>";
}

As you can see from the code above the payment gateway data can be accessed via the global WC() object. There are two different functions that return payment gateway data

  • payment_gateways() which returns all of the payment gateways that the store has access to
  • get_available_payment_gateways() which only returns the available payment gateways

In the code above a foreach loop is used to output the ids and titles of each payment method, the image below shows the output from the code

the output from the code that lists the payment gateways

How to get the Title of the Payment method used for an Order

You can do this using the get_payment_method_title() function, here’s an example

$order_id = 375;
$order = wc_get_order( $order_id );

echo 'The payment method title is - ' . $order->get_payment_method_title() . '<br>';

Final Thoughts

If you want to check what payment method was used for an order use the get_payment_method() and get_payment_method_title() functions.

You can also use the functions in if statements to run logic depending on what payment method was used (as an example you may want to add a custom message to the thank you page) If you’re not sure of the value of the id or title of  apayment method then you can check on the admin settings screen for payment gateways or loop through the  WC()-&gt;payment_gateways-&gt;payment_gateways() collection.

As always if you have nay questions or queries about this article then please don’t hesitate to let me know in the comments.


Warning: Undefined array key "format" in H:\root\home\bravenewworld101-001\www\hardworkingnerd\wp-content\plugins\convertkit\includes\class-convertkit-resource-forms.php on line 153

Leave a Comment