Difference between revisions of "How to Make a New Hook"
PeterRobert (talk | contribs) |
PeterRobert (talk | contribs) |
||
| Line 11: | Line 11: | ||
There are existing hook calls throughout the core code but a page could be copied and edited to add a hook call and saved to your selected template. | There are existing hook calls throughout the core code but a page could be copied and edited to add a hook call and saved to your selected template. | ||
| + | |||
| + | These hook calls can be seen in the files and look like e.g. | ||
| + | <pre>$OSCOM_Hooks->call('siteWide', 'injectRedirects');</pre> | ||
| + | This shows '''siteWide''', the Group and '''injectRedirects''', the Action. | ||
Revision as of 11:29, 7 March 2021
<historylink type="back">🠈 Back</historylink> 🠉 FAQS TIPS & TRICKS
The pre-placed hook calls or injection points throughout the core files enable code to be inserted or injected at those points with a hook file.
This is one of the features of CE Phoenix Cart that make it the most flexible ecommerce software available.
A hook file listens and inserts its output wherever it is called in the core code.
There are existing hook calls throughout the core code but a page could be copied and edited to add a hook call and saved to your selected template.
These hook calls can be seen in the files and look like e.g.
$OSCOM_Hooks->call('siteWide', 'injectRedirects');
This shows siteWide, the Group and injectRedirects, the Action.
Example 1
In this simple example, a message is displayed in the shop on checkout_confirmation.php.
That page is not modular, so a content module cannot be used so a hook is needed.
Looking at checkout_confirmation.php:
A hook call can be seen already in place:
echo $OSCOM_Hooks->call('siteWide', 'injectFormDisplay');
This shows siteWide, the Group and injectFormDisplay, the Action.
It can be seen that the hook call is placed just before the Finalise Order button so that is where the hook file output will be inserted.
There is already a hook file that listens for that call - MATC.php that shows the Legal agreements.
So, I need to name my new hook file according to the sorting rules explained by ecartz in the previous posts.
The rules are numbers, then uppercase letters (in alphabetical order), then _, then lowercase letters.
I want my message to show before MATC so eventually decided to name it Cmessage.php
To make the most of the template system, I created that new file in /templates/override/includes/hooks/shop/checkout_confirmation/ (I had to create the directories to follow that structure). The file could be in whatever template that is being used, that is, whatever has been selected in admin->Configuration->My Store->Template Selection. In that file I used the code: [code]<?php class hook_shop_checkout_confirmation_Cmessage {
function listen_injectFormDisplay() {
$output = null;
$this->load_lang();
$output .= MESSAGE_CHECKOUT_CONFIRMATION ;
return $output;
}
function load_lang() {
require language::map_to_translation('hooks/shop/checkout_confirmation/Cmessage.php');
}
}[/code] The class name [color=#FF0000]hook_shop_checkout_confirmation_Cmessage[/color] must follow the same structure as the hook file location, that is, shop/checkout_confirmation/Cmessage The [color=#FF0000]function listen_ [/color] is followed by the Action in the hook call, in this instance, [color=#FF0000]injectFormDisplay[/color]
The output, that is, what is shown on the page has been defined in an associated language file with [color=#FF0000]MESSAGE_CHECKOUT_CONFIRMATION[/color] So, I created a file, Cmessage,php, in /templates/override/includes/languages/english/hooks/shop/checkout_confirmation/ In that file I used the code: [code]<?php
const MESSAGE_CHECKOUT_CONFIRMATION = '
';[/code]
This results in this:
[attachment=0]Screenshot 2021-02-25 173924.png[/attachment]
So, what if I didn't want the message to show down by MATC?
I made a copy of /templates/[color=#FF0000]default[/color]/includes/pages/checkout_confirmation.php and saved that to /templates/[color=#FF0000]override[/color]/includes/pages/checkout_confirmation.php
I edited that new file (it will never be overwritten in updates) to include a new hook call in the place I want the message to appear.
In this case I added:
[code]<?php echo $OSCOM_Hooks->call('checkout_confirmation', 'message');?>
[/code]
after Line 31
https://github.com/CE-PhoenixCart/Phoenix/blob/master/templates/default/includes/pages/checkout_confirmation.php#L31
[color=#FF0000]checkout_confirmation[/color] is the Group, that is, /hooks/shop/checkout_confirmation/
[color=#FF0000]message[/color] is the Action, I could name it whatever but keep it unique and meaningful.
So now, in /templates/override/includes/hooks/shop/checkout_confirmation/Cmessage.php I change the code from: [code] function listen_injectFormDisplay() {[/code] to: [code] function listen_message() {[/code] This results in this:
[attachment=0]Screenshot 2021-02-25 181035.png[/attachment]
And I feel good!
Hooks cannot be disabled or turned on and off like content modules, they are always on but with this simple hook example, it can be made to disappear by emptying the language constant in the language file, like this: [code]<?php const MESSAGE_CHECKOUT_CONFIRMATION = ;[/code]
Phoenix Cart User Guide, like CE Phoenix Cart, is free to use but is maintained by unpaid volunteers.
All other content is the reserved Intellectual Property and Copyright of phoenixcart.org