Jump to content

Access Prestashop context (cookie) data outside of Prestashop


jwagner@abriev.com

Recommended Posts

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

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

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

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

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

  • 1 month later...

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

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 by britalb (see edit history)
  • Like 1
Link to comment
Share on other sites

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 by marxolly (see edit history)
Link to comment
Share on other sites

  • 9 months later...

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.
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

 

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

  • 4 months later...

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

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...