Skip to content
All articles

CS-Cart Hooks: What the Documentation Says and What to Check Yourself

6 min read

Hooks are how you change CS-Cart without editing core files, and editing core files is how a store ends up unable to upgrade — the argument I made in customising CS-Cart without losing the ability to upgrade. This is the practical half: how PHP and template hooks are wired, according to the official developer guide, and where that guide leaves you to check things yourself.

PHP hooks: declare, then implement

Core code exposes a hook point with fn_set_hook, passing the variables an add-on may use. The guide notes that "one function can have any number of hooks":

fn_set_hook('get_gift_certificate_info', $_certificate, $certificate, $type);

An add-on takes part in two steps. Declare the hook in the add-on's init.php:

fn_register_hooks('get_category_data_pre');

Then implement it in the add-on's func.php, in a function named fn_ + the add-on id + _ + the hook name:

if (!defined('AREA')) { die('Access denied'); }

function fn_my_addon_get_category_data_pre(&$category_id, &$field_list /* , ... */)
{
    // runs whenever core reaches that hook
}

The name is the whole binding. A typo in the function name is not an error — the hook simply never fires. If a hook seems to do nothing, check the name character by character against the fn_set_hook call first.

Hooks that only run when another add-on is on

fn_register_hooks also takes an array that names a second add-on. The guide's example registers get_news_data so that fn_seo_get_news_data is called — and only when the SEO add-on is enabled. That is the clean way to integrate with an optional add-on without checking whether it is installed inside your own code.

Where the guide is not explicit

Two things you will want to know are not spelled out in the hooks pages, so verify them in your version rather than assuming.

Whether you can change a value. The guide's own example signatures disagree: one handler takes its parameters by reference (&$category_id), another does not. Declaring a parameter by reference is what lets a change made in the handler be seen by core; whether a given hook is meant to be modified is a question for the call site. Read the code around the fn_set_hook call, then confirm with a quick test that your change actually sticks.

What the _pre suffix promises. Hook names such as get_category_data_pre follow a convention of running before the main work of a function, but the hooks pages do not define _pre and _post variants as a rule. The hook's position in the core function is the real answer, and it takes seconds to look at.

Template hooks: no declaration, just files

Templates mark extension points with:

{hook name="order_management:product_info"}
    ...
{/hook}

Unlike PHP hooks, these are not declared. In the guide's words, "a proper file naming and placing will do the trick." For the admin area:

design/backend/templates/addons/[addon id]/hooks/[template name]/[hook name].[pre|post|override].tpl

and for the storefront, under the theme:

design/themes/[theme name]/templates/addons/[addon id]/hooks/[template name]/[hook name].[pre|post|override].tpl

The three suffixes are listed in the guide without a description of exactly where each one's output lands relative to the hook body. The names point the obvious way — before, after, instead of — but render the page and look before relying on it, especially for .override, which replaces content other add-ons may also be extending.

Hooks before overrides

CS-Cart also supports replacing a whole template through an overrides directory in the add-on, which the guide describes as fully overriding the original. It is a separate mechanism from template hooks, and the blunter tool: a full copy of a core template is frozen at the version you copied, and every upstream fix to that template passes it by.

So reach for the narrowest option that works. A template hook that adds a block is better than an override that replaces the template; a PHP hook that adjusts one value is better than reimplementing the function. Each step wider is one more thing to re-check on every upgrade.

Finding the hook you need

CS-Cart publishes a hook reference, but the most reliable index is the code you are running. Search the core for the hook points themselves:

grep -rn "fn_set_hook('get_product" app/
grep -rn '{hook name="products:' design/

Reading the lines around a match tells you what variables you will receive, where in the flow you are, and whether a value is passed in a way you can change — which is most of what you need to know before writing the handler.