[email protected] Posted March 20, 2013 Share Posted March 20, 2013 I have Prestashop set up in a store directory and my webpages under the root directory. In previous versions of Prestashop, I can access the cookie data outside of Prestashop. Here is a website link that explains this: http://informatique-todo.blogspot.com/2011/10/prestashop-cookie-structure.html. I have upgraded to version 1.5.3 with the context object and cannot find a way to do this now. Any Ideas? Link to comment Share on other sites More sharing options...
vekia Posted March 20, 2013 Share Posted March 20, 2013 link you share doesnt exists you can provide cookie to external solution, but it must be on the same domain Link to comment Share on other sites More sharing options...
[email protected] Posted March 20, 2013 Author Share Posted March 20, 2013 Here is the part of the web link that is relevant: The customer cookie is read on line 94 (in PrestaShop v1.4.2) of init.php and the employee cookie is read on line 32 of admin/init.php. To access the cookie from inside PrestaShop, add global $cookie; (or add $cookie to the list of global variables) to the top of the function in a class or at the top of a non-class file. A variable in the cookie can then be accessed or changed using $cookie->variable. To access the cookie from outside of PrestaShop, use code like the following: include_once('path_to_prestashop/config/config.inc.php');include_once('path_to_prestashop/config/settings.inc.php'); include_once('path_to_prestashop/classes/Cookie.php'); $cookie = new Cookie('ps'); I want to know in my web pages if a customer is logged in, but not sure how to do that now with the context object. Anyone know? Link to comment Share on other sites More sharing options...
Paul C Posted March 21, 2013 Share Posted March 21, 2013 It should be even easier now. Try the following: <?php include('path_to_prestashop/config/config.inc.php'); $context = Context::getContext(); echo '<pre>',print_r($context->cookie, true).'</pre>'; Link to comment Share on other sites More sharing options...
[email protected] Posted March 22, 2013 Author Share Posted March 22, 2013 When I include('path_to_prestashop/config/config.inc.php'); the following statements stops the app file from completing: Context::getContext()->shop = Shop::initialize(); define('_THEME_NAME_', Context::getContext()->shop->getTheme()); define('__PS_BASE_URI__', Context::getContext()->shop->getBaseURI()); And if (defined('_PS_ADMIN_DIR_')) $cookie = new Cookie('psAdmin', '', $cookie_lifetime); else { if (Context::getContext()->shop->getGroup()->share_order) $cookie = new Cookie('ps-sg'.Context::getContext()->shop->getGroup()->id, '', $cookie_lifetime, Context::getContext()->shop->getUrlsSharedCart()); else { $domains = null; if (Context::getContext()->shop->domain != Context::getContext()->shop->domain_ssl) $domains = array(Context::getContext()->shop->domain_ssl, Context::getContext()->shop->domain); $cookie = new Cookie('ps-s'.Context::getContext()->shop->id, '', $cookie_lifetime, $domains); } If I comment these lines out in the config.inc.php then the control comes back the program that has the include. Otherwise, (with the statements included in config.inc.php, a partial page displays with the statement after the include every being reached. Any ideas? Link to comment Share on other sites More sharing options...
Paul C Posted March 22, 2013 Share Posted March 22, 2013 It seems to be the multishop code that's causing the issues. If the file is within the shop directory (or a subdirectory below that) it executes fine, but if it is outside the shop directory it redirects to the store default url..... The only way I could get it to work is to override the Shop class and then make a copy of the Shop::initialize() member function and add it to the override. I then prevented it executing the following: header('location: '.$url); exit; What I did was add a define before including config.inc.php and modified the above (in my version it's at about line 389 in the original core Shop.php file function) to be: if (!defined('_EXTERNAL_SCRIPT_')) { header('location: '.$url); exit; } The code in your script would then be: define('_EXTERNAL_SCRIPT_', 'true'); include('prestashop/config/config.inc.php'); $context = Context::getContext(); echo '<pre>',print_r($context->cookie, true).'</pre>'; So not more straightforward after all....... Link to comment Share on other sites More sharing options...
[email protected] Posted March 24, 2013 Author Share Posted March 24, 2013 Paul, thanks so much for your help. I could use a little more. I made the changes and I was able to define the context object. Unfortunately, when I check if a customer is logged in ($context->customer->isLogged()), if returns false no matter if the customer is logged in or not. Link to comment Share on other sites More sharing options...
Paul C Posted March 24, 2013 Share Posted March 24, 2013 Yes. It appears that the cookie is so deeply linked to the new multi-shop code that there's no alternative but to have your static (if they are static?) webpages in the same directory as Prestashop (or below) if you want to have them access the Prestashop cookie!! I tried hacking about with it a bit but I could only ever generate a guest cookie and never managed to access my logged in customer one.... Link to comment Share on other sites More sharing options...
[email protected] Posted March 25, 2013 Author Share Posted March 25, 2013 Unfortunately, our front end looks different that our prestashop shopping cart, so we can't put our pages under the prestashop folder. Link to comment Share on other sites More sharing options...
Paul C Posted March 25, 2013 Share Posted March 25, 2013 I'll have a further think on it as it has now become an intellectual challenge! Have drawn blanks so far though on how it could be done.... Link to comment Share on other sites More sharing options...
[email protected] Posted March 25, 2013 Author Share Posted March 25, 2013 Paul, I would appreciate any help you can give me. I've tried quite a few different things, but to no avail. Thanks. Link to comment Share on other sites More sharing options...
britalb Posted May 22, 2013 Share Posted May 22, 2013 This thread may be of use to you: http://www.prestashop.com/forums/topic/249078-integrate-header-footer-in-external-php-file/page__pid__1237837#entry1237837 It contains a more involved rewrite of the Shop class, adapted from the FAQ for the WordPress Prestacart Integration plugin: http://wordpress.org/plugins/prestashop-integration/faq/ Link to comment Share on other sites More sharing options...
marxolly Posted May 29, 2013 Share Posted May 29, 2013 It contains a more involved rewrite of the Shop class, adapted from the FAQ for the WordPress Prestacart Integration plugin: http://wordpress.org...ntegration/faq/ Thanks britalb. The solution to my problem lay in that link. I overode the cookie class and changed line 68 (in my version - 1.5.4.1) from $this->_path = trim(Context::getContext()->shop->physical_uri.$path, '/\\').'/'; to $this->_path = '/' Then I could access all aspects of the context object (including the customer object) from outside the shop directory. My shop/site is still in development, and before I go live, I would appreciate feedback from those more experienced in Prestashop about the risks/pitfalls of doing this. Cheers Link to comment Share on other sites More sharing options...
[email protected] Posted May 30, 2013 Author Share Posted May 30, 2013 I'm a bit confused now, because I am not using Wordpress at all. Can someone sum up exactly what needs to be done to access the context object values outside of Prestashop? Link to comment Share on other sites More sharing options...
britalb Posted May 30, 2013 Share Posted May 30, 2013 (edited) This isn't a Wordpress specific solution - it applies to anyone who has installed Prestashop in a subdirectory and needs to access cookie information from scripts in the root or a different subdirectory. It's pretty simple, really - Prestashop uses the PHP setcookie function to create cookies: http://php.net/manua...n.setcookie.php A cookie's path determines which directories of a website can access that cookie's data. Only scripts in a path or its subdirectories can read from or write to a cookie (this is enforced by the browser): http://en.wikipedia....Domain_and_Path by setting the path to '/' rather than the Prestashop installation directory (physical_uri.$path), you expand access to scripts in the root directory and all subdirectories. Prestashop cookies are encrypted, so any script seeking to access them will also need access to the key to unscramble the more interesting information. Still, by allowing more directories on your site to read a cookie, your shop becomes less secure. Edited May 30, 2013 by britalb (see edit history) 1 Link to comment Share on other sites More sharing options...
marxolly Posted June 1, 2013 Share Posted June 1, 2013 (edited) Yes, I'm not using Wordpress either, but have a need to determine if users are logged into (have an account) the shopping part of the site elsewhere in the site as well. Thanks britalb for the info re security. I'll keep it in mind when parsing info between scripts. Edited June 1, 2013 by marxolly (see edit history) Link to comment Share on other sites More sharing options...
[email protected] Posted June 4, 2013 Author Share Posted June 4, 2013 I tried overriding the cookies class to change the path to '/', but then I could not log into (or if I was logged in) log out of my shopping cart. Link to comment Share on other sites More sharing options...
luchiniii Posted March 31, 2014 Share Posted March 31, 2014 Did Some solve this? Link to comment Share on other sites More sharing options...
luchiniii Posted April 1, 2014 Share Posted April 1, 2014 I found a temporary solution to this in prestashop 1.5.6.1. Step 1: Create a php file in the root of your prestashop store with the next code. require(dirname(__FILE__).'/config/config.inc.php'); Tools::redirect('index.php?controller=newpage'.($_REQUEST ? '&'.http_build_query($_REQUEST, '', '&') : ''), __PS_BASE_URI__, null, 'HTTP/1.1 301 Moved Permanently'); Step2: Create a php file in the next foler PS/controllers/front/ - I called it NewPageController.php and I used it to retrieve cart products, but you can use to get anything. then copy the next code <?php class NewPageControllerCore extends FrontController { public $php_self = 'newpage'; public function initContent() { $this->display_header = false; $this->display_footer = false; if ( !isset($this->context) ) { $this->context = Context::getContext(); } $ret['num_products'] = count( $this->context->cart->getProducts() ); echo json_encode($ret); } } Step 3: Go to the folder in PS/cache/ find the file called class_index.php and delete it. You can access to the info by typing the url: http://youstore.com/index.php?controller=newpage This will work as a WebServices, retrieving the data as a JSON type. Hope It work for you. 1 Link to comment Share on other sites More sharing options...
TRE BIT Posted July 22, 2016 Share Posted July 22, 2016 I found a temporary solution to this in prestashop 1.5.6.1. Step 1: Create a php file in the root of your prestashop store with the next code. require(dirname(__FILE__).'/config/config.inc.php'); Tools::redirect('index.php?controller=newpage'.($_REQUEST ? '&'.http_build_query($_REQUEST, '', '&') : ''), __PS_BASE_URI__, null, 'HTTP/1.1 301 Moved Permanently'); Step2: Create a php file in the next foler PS/controllers/front/ - I called it NewPageController.php and I used it to retrieve cart products, but you can use to get anything. then copy the next code <?php class NewPageControllerCore extends FrontController { public $php_self = 'newpage'; public function initContent() { $this->display_header = false; $this->display_footer = false; if ( !isset($this->context) ) { $this->context = Context::getContext(); } $ret['num_products'] = count( $this->context->cart->getProducts() ); echo json_encode($ret); } } Step 3: Go to the folder in PS/cache/ find the file called class_index.php and delete it. You can access to the info by typing the url: http://youstore.com/index.php?controller=newpage This will work as a WebServices, retrieving the data as a JSON type. Hope It work for you. Hello, i tried your code but something in not working for me: if i open directly this page: http://MYDOMAIN/index.php?controller=trebitcontext it showes correctly " {"num_products":1} " Now, i need to retreive this value from a subdomain of the same domain, where i have a wordpress installation. I tried with file_get_contents('http://MYDOMAIN/index.php?controller=trebitcontext') and with CURL but the result is the same: object(stdClass)[2721] public 'num_products' => int 0 In your tutorial you wrote: "You can access to the info by typing the url: http://youstore.com/index.php?controller=newpage", how are you calling this page? Thanks Link to comment Share on other sites More sharing options...
trevorgilligan Posted November 28, 2016 Share Posted November 28, 2016 im having a problem with multistore and i think it is cookies / sessions... so MainShop.com/shop is where PS is installed. MainShop.com/shop/ShopA , works perfectly. However cant add to cart and cant log in, when i direct ShopA.com to MainShop.com/shop/ShopA ?? Can anyone help, im losing a lot of clients over this not working!! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now