set default payment method in checkout

Sometimes its helpful to have a payment method ‘preselected’ for customers in the checkout area, so they avoid getting an error message if they miss that option. Or maybe you want to select a method for them as an example of what it looks like … plenty of reasons. This short article explains how to set a default payment method in oscommerce 2.2 and cre loaded 6.4.1a carts (which is slightly harder as cre is using runoverride files now to ‘steer things in a paypal direction.’)

Oscommerce 2.2 version
Backup the file: /catalog/checkout_payment.php and edit as follows:
Find:

<?php
        if (sizeof($selection) > 1) {
          echo tep_draw_radio_field('payment', $selection[$i]['id']);
        } else {
          echo tep_draw_hidden_field('payment', $selection[$i]['id']);
        }
?>

Replace with:

<?php
          if (sizeof($selection) > 1) {
          if ($i == 0) { 
                $checked = true;          
                } else {  
           $checked = false;      
           }     
                echo tep_draw_radio_field('payment', $selection[$i]['id'], $checked);
           } else { 
                echo tep_draw_hidden_field('payment', $selection[$i]['id']);
           }
?>

Cre Loaded 6.4.1a
Backup the file: /catalog/includes/runoverride/ordercheckout/paypalxc_ordercheckout_paymentmodule.php

To get the selector bar highlighting the payment method row, modify as follows, replacing the payment method name in the ‘name of payment method’ bit:

<?php
 $radio_buttons = 0;
        $payment = (!$payment) ? 'name of the payment method' : $payment;
 for ($i=0, $n=sizeof($selection); $i<$n; $i++) {

If you’re not sure of the payment module names, go to Admin >> Modules >> Payment area, click on a module and read it’s name from the url in the address bar. Or just check in the /includes/modules/payment folder and drop the .php extension.

To get the radio button to default to the selected payment module, modify the following:

<?php
  if (sizeof($selection) > 1 || $ec_enabled ) {
   if ($i == 0) {  // defaults to payment method with id=0 NOT related to sort order in admin
      $checked = true;
   } else {
      $checked = false;
   }
  echo tep_draw_radio_field('payment', $selection[$i]['id'], $checked);
  } else {  ...

The key part in the code is the $i==0 which refers to the payment method id. This ISN’T related to the sort order of the payment modules in the Admin >> Modules >> Payment area, or some ‘name sort’ principle. Instead it just reflects the order of the modules in the database constant MODULE_PAYMENT_INSTALLED.

So if you take a look at the configuration table in your database and search for MODULE_PAYMENT_INSTALLED, your payment modules will be in a row there –
eg moneyorder.php,transfer.php,paypal.php
Here, moneyorder will have the id = 0 (so $i == 0 to be selected by default), transfer’s id = 1, paypal’s id = 2 … etc.

So by changing the $i == 0 in the code examples above to match your payment method id, you can easily set the default payment method at checkout.