Home / Archive / MemberPress: Set the order of payment methods
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Set the order of payment methods

This code lets you set the order that the payment gateways that appear on the MemberPress checkout page. The only thing that users will need to edit for this code is the $order = array(...);

The code above will show PayPal standard gateway first, followed by Stripe, Autohrize.net, and the Offline Payment. This order is set in the code on the following lines:

$order = array(
"PayPal Standard",
"Stripe",
"Authorize.net",
"Offline Payment",
);

For example, to move Stripe to the top, so it shows before PayPal and other gateways, you would modify the order of items on the above mentioned lines, in the following way:

$order = array(
"Stripe",
"PayPal Standard",
"Authorize.net",
"Offline Payment",
);

The format must match the example, with the gateway names in quotes, and a comma (,) after every gateway name.

Code Preview
php
<?php
function mepr_rearrange_payment_methods( $payment_methods, $key ) {
  //Modify the order of the payment methods below according to your needs. You can also remove non needed payment methods
  $order = array(
    "PayPal Standard",
    "Stripe",
    "Authorize.net Profile",
    "Offline Payment",
  );
  $pm_map = array();
  $mepr_options = MeprOptions::fetch();
  $option_methods = $mepr_options->payment_methods();
  foreach( $payment_methods as $pm_key ) {
    $pm = $option_methods[$pm_key];
	$pm_map[$pm->name] = $pm_key;
  }
	
  if ( count( $pm_map ) > 0 ) {
    $payment_methods = array(); //reset
	 
	foreach ( $order as $name ) {
      $next = $pm_map[$name];
	  if ( isset( $next ) ) {
		$payment_methods[] = $next;
	  }
	}
  }
	
  return $payment_methods;
}
add_filter( 'mepr_options_helper_payment_methods', 'mepr_rearrange_payment_methods', 10, 2 );

Comments

Add a Comment