Recently Updated

My Services

Blog: Controlling the Display-Order of Plugin "Module" Fields

Jun102013

When you write a plugin Payment, Shipping or Order Total module, you usually have two or more fields that the Zen Cart admin user can change to configure your creation.  For example, in the Check/Money Order payment method that is part of the Zen Cart distribution, there are 5 configuration fields, as described by the module's install function:

    function install() {
      global $db, $messageStack;
      if (defined('MODULE_PAYMENT_MONEYORDER_STATUS')) {
        $messageStack->add_session('MoneyOrder module already installed.', 'error');
        zen_redirect(zen_href_link(FILENAME_MODULES, 'set=payment&module=moneyorder', 'NONSSL'));
        return 'failed';
      }
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Check/Money Order Module', 'MODULE_PAYMENT_MONEYORDER_STATUS', 'True', 'Do you want to accept Check/Money Order payments?', '6', '1', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now());");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Make Payable to:', 'MODULE_PAYMENT_MONEYORDER_PAYTO', 'the Store Owner/Website Name', 'Who should payments be made payable to?', '6', '1', now());");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_MONEYORDER_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Payment Zone', 'MODULE_PAYMENT_MONEYORDER_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Set Order Status', 'MODULE_PAYMENT_MONEYORDER_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '0', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");
    }

The order that those fields are displayed in the Zen Cart admin are MODULE_PAYMENT_MONEYORDER_STATUS, MODULE_PAYMENT_MONEYORDER_ZONE, MODULE_PAYMENT_MONEYORDER_ORDER_STATUS_ID, MODULE_PAYMENT_MONEYORDER_SORT_ORDER and MODULE_PAYMENT_MONEYORDER_PAYTO.  How can that be, given that the sort_order column values for those fields are 1, 2, 0, 0 and 1 respectively?

The answer's quite simple ... the sort_order column has no bearing on the fields' display order!  It turns out that the display order is controlled by the array returned by the module's keys function:

   function keys() {
      return array('MODULE_PAYMENT_MONEYORDER_STATUS', 'MODULE_PAYMENT_MONEYORDER_ZONE', 'MODULE_PAYMENT_MONEYORDER_ORDER_STATUS_ID', 'MODULE_PAYMENT_MONEYORDER_SORT_ORDER', 'MODULE_PAYMENT_MONEYORDER_PAYTO');
   }

Once you know the rules, it's easier to play the game!

Copyright © 2012-2021 Vinos de Frutas Tropicales. Powered by Zen Cart