Joomla 4 - How to save values in module cookies on the site

147 views
Skip to first unread message

Alexander Berezuev

unread,
Dec 18, 2021, 7:36:45 PM12/18/21
to Joomla! General Development
Hello, I have a site is a module with the notification of cookies, but the developer, who made me it has long been out of work and does not want to do me adaptation to Joomla 4. I myself began to do the adaptation, but it does not work saving values in cookies.

There is Javascript code on the website page:

jQuery(document).ready(function($){
        $('.iws-button-accept').click(function(){
            $.ajax({
                url: '/index.php?option=com_ajax&module=cookie_notice&format=json',
                type: 'post',
                success: function(){
                    $('.cookie-notice').hide();
                }
            });
        });
    });

The module cookie_notice has a getAjax code in the /Helper/CookieNoticeHelper.php file.
public static function getAjax(){
        $module = ModuleHelper::getModule('cookie_notice');
        $inputCookie  = Factory::getApplication()->input->cookie;
        $params = new Registry($module->params);
        $cookie_time = $params->get('cookie_time');
        $cookie_name = $params->get('cookie_id');
        $inputCookie->set($cookie_name, 1, time() + 3600 * 24 * $cookie_time);
        return true;
    }

In the module's template is checking for cookies, if there is no value, it shows a window with a notification.
$inputCookie = Factory::getApplication()->input->cookie;
$value_cookie = $inputCookie->get($name = $cookie_name, $defaultValue = null);
if($value_cookie != 1){
... here is the notification window But the code does not save the value of the cookies and constantly shows the window to the visitor, although the visitor accepts the rules.
Please tell me where the bug is in the code, so that cookies are saved and everything works.
Thank you.
Reply all
Reply to author
Forward
0 new messages