React to consent in PHP
On the server, the actionKixellTagConsent hook tells the module when the visitor's choice changes, and the hasVendorConsent() method answers on demand. Neither requires Kixell Tag to be installed.
No Kixell Tag class needs to be included: everything goes through the PrestaShop hooks and the instance of the kixelltag module. Without Kixell Tag, the hooks are never called and the method answers no: the module collects nothing.
Registering the hooks #
Register actionKixellTagRegisterVendors and actionKixellTagConsent in install(), as in Declare the partners. PrestaShop accepts these hooks even if Kixell Tag is not installed yet: they will be called as soon as it is. For a module already in place, add an upgrade script: see Integration recipes.
Warning
Do not write $this->dependencies = ['kixelltag']: PrestaShop would refuse to install your module without Kixell Tag.
The actionKixellTagConsent hook #
Kixell Tag calls this hook when the visitor's choice changes. The module reacts to it: it deletes its data on a no, and has nothing heavy to do on a yes.
public function hookActionKixellTagConsent(array $params)
{
try {
if (!empty($params['modules']['mymodule'])) {
return; // Consent: the page script or a controller starts the collection.
}
// Refusal or withdrawal: delete this visitor's data, then your cookie.
} catch (Exception $e) {
PrestaShopLogger::addLog('mymodule consent hook: '.$e->getMessage(), 3);
}
}
| Parameter | Type | Content |
|---|---|---|
origin |
string | choice: the visitor has just made a choice. page_load: on a page, the recorded choice differs from the last state passed on in this browser |
id_shop |
integer | The shop of the request |
vendors_consent |
array of strings | The identifiers of all accepted partners: kpv:12, iab:755, kcv:143… |
modules |
key => boolean array | Consent to each partner declared by a module, by key: ['mymodule' => true]. A missing key means that Kixell Tag has not created this partner yet: treat it as a no |
first_choice |
boolean | With choice only: the visitor had no recorded choice before. Always false with page_load |
previous_vendors_consent |
array or null |
With choice only: the partners accepted before this choice. null without a previous choice and with page_load |
consent_uid |
string | The visitor's identifier in the Kixell Tag consent register. It is personal data: keep it with your data if you need to prove consent, do not log it |
There is no previous state per key. To know whether consent to your partner was withdrawn, and not just never given, look at your own trace: your cookie, your rows for this visitor.
When it is called
origin |
Moment | Request |
|---|---|---|
choice |
Right after a choice is recorded, including a choice made on a linked WordPress site that shares the shop's consent | The request that records the choice: the banner waits for its response |
page_load |
On a page, when the recorded choice differs from the last state passed on in this browser | Without the Do you use a caching system? setting: the page itself. With this setting: the asynchronous call the page makes |
It is not called for a visitor with no recorded choice, for bots and excluded IP addresses, when the banner is turned off, or for a state already passed on in this browser. It therefore runs about once per browser and per change of choice. It is a signal to act, not a way to read consent on every page.
A partner declared by a module after the choice changes the state passed on: the hook is then called once more, with page_load.
Rules inside the hook
- Be fast. With
choice, the banner waits for the response: no remote call, no heavy query. - Display nothing. Kixell Tag ignores anything the hook outputs.
- Catch your exceptions. Kixell Tag catches and logs those that reach it, but your own logs remain clearer.
- Do not call
hasVendorConsent()here. Withchoice, the visitor's cookies still contain the old choice. Everything is in$params. - You can set or delete a cookie with
setcookie(), after checkingheaders_sent().
The hasVendorConsent() method #
It answers at any time: has the visitor given consent to this partner? Pass the key your module declared, or the identifier of any partner (kpv:12, iab:755).
/** @var bool[] */
private static $consentCache = [];
/**
* Visitor's consent to a partner, read from Kixell Tag. No without Kixell Tag,
* without a recorded choice or in case of error.
*
* @param string $key Declared key ('mymodule') or identifier ('iab:755')
*
* @return bool
*/
public static function hasConsent($key = 'mymodule')
{
if (!isset(self::$consentCache[$key])) {
$consent = false;
if (Module::isEnabled('kixelltag')) {
$kixelltag = Module::getInstanceByName('kixelltag');
$consent = $kixelltag && method_exists($kixelltag, 'hasVendorConsent')
&& (bool) $kixelltag->hasVendorConsent($key);
}
self::$consentCache[$key] = $consent;
}
return self::$consentCache[$key];
}
- It returns
trueonly if the visitor's recorded choice enables this partner: see How the choice becomes yes or no. - It returns
falsewithout a recorded choice, when the banner is turned off, for an unknown key, for a partner that is not ticked and in case of internal error. - Keep the wrapper above: Kixell Tag may be missing, disabled or too old. No answer, no consent.
- Keep the answer in memory for the request if you call it often: it reads and decodes the consent token on each call.
Where its answer is reliable
The method reads the visitor's cookies in the current request. Its answer is only accurate in a request that comes from their browser and that no cache shares between visitors.
| Context | Reliable? |
|---|---|
Module front controller (ModuleFrontController), AJAX or POST call from the page |
Yes: PrestaShop never caches them |
Cart, checkout, customer account, actionCartSave, actionValidateOrder during the visitor's order |
Yes |
Hooks that produce the HTML of catalogue pages (displayHeader, displayFooter, product page, category) |
Only without a page cache: a full page cache (cache module, Varnish, LiteSpeed, CDN) serves one visitor the HTML computed for another |
| Payment notifications (IPN, webhooks), scheduled tasks, command line | No: there is no visitor cookie, the answer is always no |
For these last two cases, see Browser side and page cache and Orders and payment notifications.
Missing or wrong information? Tell the support team