Zen Cart Logo
Forums / General Questions / Common Notifier for all Pages?

Common Notifier for all Pages?

Views: 554

Results 1 to 5 of 5
3 Apr 2016, 8:49 PM
#1
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Common Notifier for all Pages?

I've been working on a sidebox to allow selected customers to immediately download a free file. I've installed observer code to process the submit button that initiates the download and used header_start notifiers to kickoff the observer code. However, some page headers don't have a header_start notifier. The featured products page is one example of several page headers without a notifier. In addition, the list of header_start notifiers is long.

Is there a single notifier common to all pages that exists before headers are written? If I pick say a notifier in the sidebox path, headers have already been written and I get a "headers already written" error.

If not, can I add a header_start_xxx notifier in the header file of the pages that don't have notifiers? Do I just add the one line notifier call to the header files or are other files involved that need modification? I would also add the header_start_xxx notifier to the observer code. Must the notifier name be unique or can the same notifier exist in multiple files?

Thanks,
Dave

3 Apr 2016, 9:24 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,065
Plugin Contributions:
56

Re: Common Notifier for all Pages?

There's (unfortunately) nothing set in the core-code. You could try an alternate path: create an init_include or (kind of cheating) an extra_functions file.

If you go down the "proper" init_include route, you'll need to create an auto-loader for that file (/includes/auto_loaders/config.your_special_name.php) that (er) loads the initialization file (/includes/init_includes/your_special_name.php. You get to choose when it's loaded (code-point 200 is after all the Zen Cart core is loaded).

If you go down the simpler extra_function route, all you need to create is the extra_functions php file. All the .php files present in /includes/functions/extra_functions are loaded prior to loading the header_php.php file for the active page (so, prior to headers being written). The limitation is that the extra_functions are loaded fairly early in the page-rendering process and you don't have control over when the file is loaded.

4 Apr 2016, 1:17 AM
#3
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Common Notifier for all Pages?

You'd have more options if you were using v155 instead of v151.

But one way to retrofit v151 to get a hook that happens before any of the header_php.php files fire is to copy the v155 version of /includes/init_includes/init_canonical.php and replace your v151 version with that one. It's backward-compatible, and offers a hook called NOTIFY_INIT_CANONICAL_FINAL
Unfortunately v151 has less flexibility regarding passed variables, but it can at least listen for that hook, even though it can't pass any updates back.

Or, you could just put a new file in the extra_functions folder, containing an "attach" to your own unique notifier hook. (Or Lat9's suggestions work too, but with more files)

4 Apr 2016, 1:22 PM
#4
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Common Notifier for all Pages?

Thank you both for your valuable suggestions; each is very elegant. I'm in the process of updating to v155 and can't wait to get there, but I need to complete this mod first. I'll report back with my results.
Dave

4 Apr 2016, 2:03 PM
#5
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Common Notifier for all Pages?

I decided to go with DrByte's init_canonical approach. Took just a few minutes to replace the file and change the notifier in the observer code. Worked perfectly! Thanks again.
Dave