Populate coupon code on signup page from PHP session
First, ensure that the signup form includes the Coupon brick. Then add the following code to the site.php file:
$_SESSION['signup_coupon'] = 'TEST';
Am_Di::getInstance()->hook->add(Am_Event::BEFORE_RENDER, function (Am_Event $event) {
$template = str_replace('\\', '/', $event->getTemplateName());
if (strpos($template, 'signup/signup') === false) {
return;
}
$couponCode = $_SESSION['signup_coupon'] ?? '';
$view = $event->getView();
if (!$couponCode || empty($view->form)) {
return;
}
$elements = $view->form->getElementsByName('coupon');
if ($elements && !$elements[0]->getValue()) {
$elements[0]->setValue($couponCode);
}
});
Replace TEST with the coupon code you want to populate. If another script already stores the coupon code in the
PHP session, remove the first line and use that script's session key instead of signup_coupon.