How Add-Ons Work?

Hello..

Can someone please exlpain me the way the add-ons work?

I have read documentation, I read the tutorial but still have questions..

I create addon.xml. I create init.php in which I declare my hooks, I create func.php in which I put code for my hooks.

Right?

If yes, how can I then use these hooks?

I am stuck!

Thank you very much in advance.

For example, there is the get_product_data_post hook in the fn_get_product_data function (app/function/fn.catalog.php)

- Create the addon.xml file and install the module

- Create the init.php file to register your hook

- Create the func.php file with the fn_[ADDON_ID]_get_product_data_post function

- Pass arguments by reference. E.g.

function fn_my_changes_get_product_data_post(&$product_data, &$auth, &$preview, &$lang_code)
{

}

- Change $product_data array

- Check the result

For example, there is the get_product_data_post hook in the fn_get_product_data function (app/function/fn.catalog.php)

- Create the addon.xml file and install the module

- Create the init.php file to register your hook

- Create the func.php file with the fn_[ADDON_ID]_get_product_data_post function

- Pass arguments by reference. E.g.

function fn_my_changes_get_product_data_post(&$product_data, &$auth, &$preview, &$lang_code)
{

}

- Change $product_data array

- Check the result

Hmm.. Ok I understand this.

If I want to create my own hooks, where do i need to call them?

Let's say I want a hook that checks the database if the e.g. email that user typed, exists in db and if it exist, redirect him to another page.

I will create the function in the func.php file. I can undestand this.

What do I need to do to show the user a form to input his email for example?

Some code in the design\themes\MY_THEME\templates\addons in .tpl file?

Hmm.. Ok I understand this.

If I want to create my own hooks, where do i need to call them?

Let's say I want a hook that checks the database if the e.g. email that user typed, exists in db and if it exist, redirect him to another page.

I will create the function in the func.php file. I can undestand this.

What do I need to do to show the user a form to input his email for example?

Some code in the design\themes\MY_THEME\templates\addons in .tpl file?

You do not have to use only hooks. You can also create your own controllers or post/pre controllers.

What do I need to do to show the user a form to input his email for example?

You can create the controller if there is new page, for example, app/addons/my_changes/controllers/frontend/custom_form.php, then create the template for this controller: design/themes/YOUR_THEME/templates/addons/my_changes/views/custom_form/MODE.tpl, where you will place the form for the e-mail.

If you want to add the form to the existing page, then you need to use pre/post controllers and hooks in templates.

You can find many examples of the default add-ons and examine how they work. Just start to create your add-on and you will understand how it works. Of course, we will help you.

If I want to create my own hooks, where do i need to call them?

Own hooks can be added to

- custom php functions

- custom, pre, post controllers

- custom templates

You do not have to use only hooks. You can also create your own controllers or post/pre controllers.

You can create the controller if there is new page, for example, app/addons/my_changes/controllers/frontend/custom_form.php, then create the template for this controller: design/themes/YOUR_THEME/templates/addons/my_changes/views/custom_form/MODE.tpl, where you will place the form for the e-mail.

If you want to add the form to the existing page, then you need to use pre/post controllers and hooks in templates.

You can find many examples of the default add-ons and examine how they work. Just start to create your add-on and you will understand how it works. Of course, we will help you.

Thank you very much Oleg.

I am thinking of creating a new login page for my user but because I want to make the login process in two steps,

First Step:

user gives email, I check

1) if it exists and if not, I want to redirect him again to input a valid email address for example,

Second Step:

1) if it exists only one time, so I ask him for the password and

2) if it exists more than one time, so I ask him to pick a "user" and to type the password.

I want to make it as an addon in order to leave the core code intact.

So, if I understood well what you said, I must create app/addons/my_changes/init.php and app/addons/my_changes/func.php

One question here is, is it possible to not declare any hook on init.php and func.php?

I ask this because you said that I can use controllers.

So if I am going to create controllers, I have to create a app/addons/my_changes/frontend/controllers/custom_form.php in which I will have my "modes" to specify what the form must do in every form.

And then, I have to create a design/themes/MY_THEME/templates/addons/my_changes/views/custom_form/MODE.tpl

in which I have to write the form that asks for the email as you said.

I followed some advice from the forums and I understood these:

An addon is not necessary to have the init.php and func.php files to show something on screen. (app/addons/my_changes)

I managed to show an email input area by doing these:

I created a "my_changes.php" in the following location:

app\addons\my_changes\controllers\frontend

if ($mode == 'my_info')
{
    return array(CONTROLLER_STATUS_OK);
}

and n design\themes\MY_THEME\templates\addons\my_changes\views\my_changes I created a file named: "my_info"

{__("email")}

{hook name=“index:login_buttons”}



{include file=“buttons/continue.tpl” but_name=“dispatch[auth.login]” but_role=“submit”}


{/hook}

and now, if I go to "localhost/eshop/index.php?dispatch=my_changes.my_info " I get a page with a text area for my user to fill in with his/her email and a button which for now does not working but I'm happy I have managed something today :)

Note that you forgot to add form to the template. As a result, button does not work

Note that you forgot to add form to the template. As a result, button does not work

How can I add the form in the template? Thanks

How can I add the form in the template? Thanks

For example, like this:

    
{__("email")}
{hook name="index:login_buttons"}
    
{include file="buttons/continue.tpl" but_name="dispatch[auth.login]" but_role="submit"}
{/hook}

Thanks. The button now works but what should I do if I want to change the process the button makes?

Create a new button or edit the existing?

Thanks. The button now works but what should I do if I want to change the process the button makes?

Create a new button or edit the existing?

Edit the existing. You will need to change "auth.login" to your own controller and mode.

You mean to change the existing auth.login mode in the app\controllers\common\auth.php file?

Or to take this auth.php in the app\addons\my_changes\controllers\frontend and modify it there?

Edit the existing. You will need to change "auth.login" to your own controller and mode.

As a example, you can change dispatch on the button to auth.login2

Then create auth.pre.php controller and use the following condition:

if ($mode == 'login2') {
    .... your code here ....
}

You mean to change the existing auth.login mode in the app\controllers\common\auth.php file?

Or to take this auth.php in the app\addons\my_changes\controllers\frontend and modify it there?

You have already created the my_changes.php file, so you can just create the mode in POST section and use it.

As a example, you can change dispatch on the button to auth.login2

Then create auth.pre.php controller and use the following condition:

if ($mode == 'login2') {
    .... your code here ....
}

Where should I create the auth.pre.php? In app/addons/my_changes/controllers?

You have already created the my_changes.php file, so you can just create the mode in POST section and use it.

What do you mean by POST section?

To justify in my my_changes.php file what to do while the button is pressed?

Where should I create the auth.pre.php? In app/addons/my_changes/controllers?

What do you mean by POST section?

To justify in my my_changes.php file what to do while the button is pressed?

You can read more about controllers here: http://docs.cs-cart.com/4.4.x/developer_guide/core/controllers/index.html

I understood what I have to do for this.

I managed to create a new mode in the right way.

$user_data holds what user typed in my field?

If not, which variable does that?

I understood what I have to do for this.

I managed to create a new mode in the right way.

$user_data holds what user typed in my field?

If not, which variable does that?

You can print all received data to screen with the following code

fn_print_die($_REQUEST);

This command was a huge help thank you!! :D