Category Archives: functions

tips on how to change, improve how osCommerce-based cart functions

how to upgrade oscommerce-based scripts from php 5.2.x to php 5.3.x

More and more hosting companies are upgrading their servers to run php 5.3. For most carts, this will mean an error log file full of deprecated error messages – warnings that one day, the functions being used will be removed entirely and the script will break.

If you’re running an older version of your oscommerce-based cart software (eg Cre Loaded 6.4.1, Oscommerce 2.2 etc) you will find that your error log file will fill up with deprecated messages from a number of files still using old calls like ‘ereg’ and ‘ereg_replace.’

You could switch these error messages off if you can control the error_reporting configuration on the server you’re on, but there’s a chance that when php 5.4 is adopted by hosting companies (RC6 of this was released January 2012) many of these deprecated messages now will become broken scripts then.

Updating most of these old calls can be fairly easy – here is a reference table that will help:

ereg() = preg_match()
ereg_replace() = preg_replace()
eregi() = preg_match() with the 'i' modifier
eregi_replace() = preg_replace() with the 'i' modifier
split() = preg_split()
spliti() = preg_split() with the 'i' modifier

As an example – cre loaded 6.4.1a B2B file /includes/functions/general.php :

if (ereg('^[0-9]+$', $value)) {

would become

if (preg_match('/^[0-9]+$/', $value)) {

(note ereg becomes preg_match and the forward slashes (delimiters) are added in)

Other deprecated functions and directives are more involved and may in fact only be configurable by the hosting company if they don’t allow custom php.ini files. There are other examples in the code that can simply be removed to upgrade to php 5.3.x and will stop the deprecated message.

A popular old check in Cre Loaded is a 6.2 version check – this from /admin/includes/runtime/orders/RC_orders_boxesbottom.php:

if (defined('MODULE_ADDONS_RECOVERCARTS_STATUS') && MODULE_ADDONS_RECOVERCARTS_STATUS == 'True') {
  if (defined('PROJECT_VERSION') && ereg('6.2', PROJECT_VERSION)) {
    $rci = tep_admin_files_boxes(FILENAME_RECOVER_ABANDONED_CARTS, BOX_RECOVER_ABANDONED_CARTS, 'SSL','tdate=' . $tdate, '0');
  } else {
    $rci = tep_admin_files_boxes(FILENAME_RECOVER_ABANDONED_CARTS, BOX_RECOVER_ABANDONED_CARTS, 'SSL','tdate=' . $tdate, '2');
  }
}

In this case the ‘upgrade’ would be to remove the old version check (bypassing the need for the deprecated ereg check.)

Third-party modules may cause grief when your hosting company upgrades to php 5.3.
At the time of writing this, Magneticone’s modules that use Zend Optimizer for decoding will break as Zend hasn’t provided a backward compatible version of the ZO for scripts using php 5.2.x. Magneticone’s advice in this situation is to use the ioncube versions of the scripts only (as they haven’t upgraded their modules to use the standalone ZO php 5.3 version either.) Nuisance.

If you’re unsure about these changes, contact me for a quote to help you upgrade from php 5.2 to php 5.3

put lists in a dropdown in alphabetical order

Adding new infoboxes through the Admin of an oscommerce cart is laborious enough without having to hunt through an unsorted list of filenames in a dropdown. So this article’s short tip examples one of php’s really useful functions – sort().

This is an unsorted list of infobox files in a dropdown in a Cre Loaded Admin:
screenshot from cre loaded 6.4.1a of unsorted list in dropdown of infobox filenames

Same list now sorted alphabetically:
screenshot of list of filenames sorted alphabetically

The basic code change required to an array:

 $dirs1[] = $file;
            $dirs_array1[] = array('id' => $file1,
                                   'text' => $file1);
          }
        }
        closedir($handle1);
      }

sort($dirs_array1);  // added to sort list alphabetically

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.

how to install ajax country state selector

The address book feature of oscommerce-based carts is fairly weak in some areas. This modification and free code module will help in the new account creation area by changing the clunky ‘choose a country, type in a state’ method of the current carts* to one that uses ajax to pull a list of states from the database based on the selected country. And by default it will load the store’s country (s most traders do in fact sell mostly to their own country.)

(* In fact the default cart behaviour in oscommerce 2.2 and cre loaded is to make you GUESS the spelling of the state and then if you get it wrong it’ll throw an error and THEN show you a dropdown list of states from the database. Madness.)

Ajax Country State Selector
(cre loaded port, from oscommerce contribution country state selector by steve lionel)

Written for:
any version of cre loaded 6.2.x -> 6.5.x, but easy to convert for all osCommerce-based carts (including oscMax, zencart)

Video Demo:


What it does:

Ajax Country State Selector improves the address handling functions of the cart and makes it quicker and easier for customers to create and edit addresses, whether in their account or in the checkout. The store’s default country and any states of that country are loaded with the page. On change of country, any states associated with that country will be loaded too.

Ease of use:
Should know how to check and setup zones if missing from cart. Otherwise, nothing to do.

Limitations:
The code provided ONLY shows a modified create account page – there are several other address areas in the cart that would need to be edited if this functionality was required for all screens. However, if new customers can get their accounts setup correctly then some of the subsequent address issues may be avoided.

The other real possibility is that the ajax functionality fails. There can be many reasons for this, and may be caused by conflict with a highly modified template that already uses javascript extensively. NO GUARANTEES are provided with this script.

Experience needed to install:
Beginner – upload 3 files, edit 5 files.

Don’t want to install it yourself?
No problem – drop me an email and I can install it for you at a minimal price.

 

how to modify the visual verify code (VVC) system

The visual verify code (or VVC) is a feature of cre loaded and other oscommerce-based carts and is designed to reduce the amount of spam and automated bot abuse of a store’s email system. It can appear (and be enabled/disabled through the admin) in several parts of the cart, each involving some sort of form submission to the owner of the store. So it’s an attempt to prove that it is a person sending the email and not a crawler or automated script.

In cre loaded, it is used for:
– password recovery
– creating a new account
– contacting the store owner via contact us
– product or article review submission
– sending tell-a-friend emails about a product or article
– submitting a link

However, it’s effectiveness is limited and in some cases it can be more hassle than it’s worth – eg sometimes the vvc code is difficult for us humans to read due to similar characters or the size of the display etc. These modifications may help if you’re experiencing problems.
image of visual verify code modifications to cre loaded vvc

#1 – Change the size of the pool of characters that the code can be drawn from
In some fonts upper- and lowercase I i L l as well as O o and the number 0 can appear very similar and can cause confusion. The pool of letters and numbers the visual verify code system uses is defined in the file /includes/languages/english/english.php :

/* REDUCE THE VVC POOL OF ALPHANUMERICS TO NUMBERS ONLY */

original:
define('VISUAL_VERIFY_CODE_CHARACTER_POOL', 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz123456789');  //no zeros or O

replace with:
define('VISUAL_VERIFY_CODE_CHARACTER_POOL', '123456789');  // numbers only

The replacement pool of only numbers may seem drastic, but it is a lot easier for customers to verify the code correctly.

#2 – Change the size of the input box where the code is to be entered

/* CHANGE INPUT BOX SIZE TO 15 */

<td class="main"><?php echo tep_draw_input_field('visual_verify_code', '', 'size="15"') . '&nbsp;' . '<span class="inputRequirement">' . VISUAL_VERIFY_CODE_ENTRY_TEXT . '</span>'; ?></td>

/* ADMIN CONTROL OF INPUT BOX SIZE */

<td class="main"><?php echo tep_draw_input_field('visual_verify_code', '', 'size="' . VVC_INPUT_SIZE . '"') . '&nbsp;' . '<span class="inputRequirement">' . VISUAL_VERIFY_CODE_ENTRY_TEXT . '</span>'; ?></td>
                

This will tidy up the VVC area and also supports a reduced number of characters being used. Various template or catalog files would need to be edited here, as per the list given above. This means by default you’d need to change this in each occurance of the VVC code. A way to improve this is to create a switch in the VVC configuration area of the admin, and set the input box width there (using the second block of code above, together with the sql query below.) You would still have to edit several template related files, but from then on changing this setting in the admin page would change them all.

INSERT INTO `<<the name of your database goes here>>`.`configuration` (`configuration_id`, `configuration_title`, `configuration_key`, `configuration_value`, `configuration_description`, `configuration_group_id`, `sort_order`, `last_modified`, `date_added`, `use_function`, `set_function`) VALUES (NULL, 'VVC Input Box Size', 'VVC_INPUT_SIZE', '15', 'Size of the VVC input box', '420', '9999', '0000-00-00 00:00:00', '0000-00-00 00:00:00', NULL, NULL);

#3 – Reduce the length of the VVC code and width of the VVC Image box
By default, from 3 to 6 alphanumeric characters are displayed as the visual verify code. By changing the numbers, a different range can be used:

/* RANGE OF NUMBER OF VVC CHARACTERS TO DISPLAY */

By default - 3 to 6 characters:
for ($i = 1; $i <= rand(3,6); $i++){

Here - 3 to 4 characters:
for ($i = 1; $i <= rand(3,4); $i++){

After reducing the number of characters displayed, you may need to resize the width of the code box. This is done easily through admin >> configuration >> vvc configuration >> VVC Image Width – in the example, this was set to 125.