Search code examples
phphooksmartyprestashop-1.6

Passing Variable Between Hooks in Same Class File


I am trying to retrieve a value from one action hook to be displayed in admin page.

public function hookActionProductCancel($params)
{
    $this->response = "response";
}
public function hookDisplayAdminOrder($params) {
    $this->context->smarty->assign('response', $this->response);
    return $this->display(__FILE__, 'views/templates/admin/response.tpl');
}

I am not receiving the value "response" in response.tpl. Probably a small issue but I am not getting it right at the moment.

Any guidance is truly appreciated. Thank you.


Solution

  • You should just store the response to cookie and clear it before displaying it.

    public function hookActionProductCancel($params)
    {
        // code
        $this->context->cookie->mymodule_response = "response";
        $this->context->cookie->write();
    }
    public function hookDisplayAdminOrder($params) 
    {
        // if no response stored in cookie do nothing
        if (!$this->context->cookie->mymodule_response) {
            return false;           
        }
    
        // assign response from cookie to smarty then clear response from cookie
        $this->context->smarty->assign('response', $this->context->cookie->mymodule_response);
        unset($this->context->cookie->mymodule_response);
    
        return $this->display(__FILE__, 'views/templates/admin/response.tpl');
    }