Creating Extension from Scratch

From osCommerce Wiki
Revision as of 16:11, 8 September 2022 by Admin (talk | contribs) (Created page with "== Intro == Extension is the tool that allows to deploy not only the minor basic function modifications, but also create the independent extensions based on the system. In thi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Intro

Extension is the tool that allows to deploy not only the minor basic function modifications, but also create the independent extensions based on the system. In this lecture we will review all the extension elements in detail and deploy them in our first extension on practice. We recommend to get familiarized with this whole article. Even without having the great knowledge of the system you will be able to create your own extension sooner or later. But following the general principles will allow the majority of developers to understand your code quickly and easily.

First look

Conceptually extension is the system mini copy. By engaging all its means you will be able to create the frontend analogue easily or create the report for backend, based on the data analysis.  

Image 1


On the image 1 (above) you can see the general architecture scheme of the extension constituent parts. For easy perception all the parts are divided into 4 categories.

The founding elements are indicated as the Basic functionality. This is the minimum set that we will require to perform the simplest manipulations within the system.

First of all, the functions are the static methods, realized in the main extension class or the traits if your extension allows it. Usually, the methods are not intended for querying directly, but, if necessary, backend allows to do it via the special controller extensions?module=YourExtensionName&action=adminAction. For frontend such approach is considered to be unsafe and is unavailable.

The next founding element is the hooks. These are the access points, allowing to execute your code in different places. Hooks can be as both the php file and the tpl template. As a rule, the similar files are placed in the subdirectory hooks and are described in the static method getAdminHooks in the main class or in the installing class which we will review later. You can always find the list of the available hooks within the system by checking the file /lib/common/extensions/methodology.txt And the final element of the first extension part is render. This is the template class processor. We did not make it general to provide with enough freedom for realizing within extension. It is worth mentioning that usually this class Render is located in the root extension folder. In this case the template files should be placed in the directory view on the same level.

Image 2


On practice all the three elements organize the chain of the sequential actions as on the image 2.

First step

We suggest you to realize the following idea: the additional field on the customer editing page for backend. To make it simple let it be the text field. An administrator will be able to fill in a word for defining a customer, for example a rank. It is enough for us at the moment.

Thus, let us start working on this task. We assume that our new field will be called Rank. The whole task comes down to adding some classification of these ranks, let us call our extension Customers rank.

After reviewing the system architecture you should be familiar with the start point for any extension. This is the folder /lib/common/extensions/ where all the extensions regardless of their purpose are located. Let us define the place for our new extension and create the new folder. It should be taken into account that the words beginning with the capital letter and without spaces in the title, so called CamelCase. Then we create the folder CustomersRank. Within this folder we create the file CustomersRank.php which content will look in the following way: