Jump to content

Set a Global variable accessible in a my module


AlainGau

Recommended Posts

I created a module with a hook on hookActionCartSave, which work very well. 

 

Then I  created a simple php file in same folder has my module that listent to a POST and I try to save a parameter value in global variable that my hook could get back once the use add something to the cart (i.e. tirgger hookActionCartSave)

 

 

 

1. My new file (setParam.php) has the following very simple code, it takes the POST parameter 'param1' and put his value in superglobal $_ENV ,   var_dump($_ENV["param1"]); show me that it sets it correctly.  

 

<?php
define('_EXTERNAL_SCRIPT_', 'true');
 
include(dirname(__FILE__).'../../config/config.inc.php');
include(dirname(__FILE__).'../../init.php');
include(dirname(__FILE__).'../../classes/Cookie.php');
 
$_ENV["param1"] = $_POST['param1'];
  var_dump($_ENV["param1"]);
?>
 
2. But then I my module $_ENV is always empty, it seems that $_ENV is not shared or ...??
...
function hookActionCartSave($params) {   
    var_dump($_ENV["param1"]);
}
...
 
I tried with $_COOKIE, $GLOBALS, $_SESSION and I not able to add value to global context from my setParam.php file. 
 
Any idea how I can add value to the current session context from a standalone file that receive a post from the UI (jquery ajax post)?
 
Thanks - Alain
 
 
 
Link to comment
Share on other sites

You need use cookies. If you catch some values by ajax, you are executing one script but this is finished, try this in the php file called by ajax:

Context::getContext()->cookie->__set('key_value', $_POST['param1']);

Now get the cookie in the hookActionCartSave:

$this->context->cookie->key_value
Edited by COTOKO (see edit history)
Link to comment
Share on other sites

Hi

 

 

You need use cookies. If you catch some values by ajax, you are executing one script but this is finished, try this in the php file called by ajax:

Context::getContext()->cookie->__set('key_value', $_POST['param1']);

Now get the cookie in the hookActionCartSave:

$this->context->cookie->key_value

 

 

hi Cotoko, 

 

I does not recognize the context, I am getting the following error: 

Class 'Context' not found in <b>/presta/modules/mymodule/setParam.php

 

What am I missing to get the context? I have included the config, init and cookies....

 

I am running PS 1.6

Edited by AlainGau (see edit history)
Link to comment
Share on other sites

 

ok it works, I path where wrong, from module folder should be: 

 

include('../../config/config.inc.php');
include('../../init.php');
include('../../classes/Cookie.php');

 

Ok, if you have solved your issue, please try to edit the thread and add the word (solved) at the start of title.

Link to comment
Share on other sites

  • 1 year later...

Hi, can you please help me? I have module, where I asign few variables

 
$settings = array(
'var1' => (Configuration::get('VAR1')), 
'var2' => (Configuration::get('VAR2'));
$this -> context -> smarty -> assign('vars', $settings);
 

and then use it in tpl files like this: 

{$vars.var1}

 

It works. And now the problem:

I need to use some vars in the product-list.tpl, so after filter or sorting - vars become undefined, I think because of ajax content reload.

Can you tell what code I should use to define these vars as cookies vars, and how to call it in product-list.tpl ? Thank you in advance.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...