Author Topic: Easiest way to know what is the customer_id from "outside" the AbanteCart env  (Read 137 times)

Offline teppyogi

  • Newbie
  • *
  • Posts: 6
  • Karma: +1/-0
    • View Profile
Hi again,

I wish to include, outside of the AbanteCart architecture, a php file which can provide information based on the customer_id, if one is logged in. I have found online that many extensions use the following call:
Quote
$this->session->data['customer_id']

However, this seems to be for certain components which inherit some class from the core, I imagine. What I would like to know is the simplest way to simply be able to retrieve that information without going through a formal "extension" or "plugin".

In other words: I would like to add a file test.php at the root of the domain which can retrieve the customer_id.

Is this possible or is a formal development of an extension the only way? Thanks !

Tepp

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1906
  • Karma: +290/-11
  • web for all, all for web!
    • View Profile
    • AbanteCart
hi,
not sure understood you corrrectly.

Abantecart session data contains customer id of logged customer.
Is your "outside" app will run inside this session?

You can play with a admin-side code that allow to act-on-behalf
(see admin/controller/pages/sale/customer.php  method actonbehalf())
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

Offline teppyogi

  • Newbie
  • *
  • Posts: 6
  • Karma: +1/-0
    • View Profile
Thanks, I took a look at the php file and method you suggested, I'm afraid it's my turn to not be sure I understand :-)

I see a bunch of bits which look interesting in there (e.g. $this->request->get['customer_id']), but the problem is I don't know how my php code can instanciate the "$this". I am too much of a beginner with respect to MVC frameworks, so I am hoping to simply be able to "get the cookie / read it", perhaps using some premade AbanteCart functions, in order to know who is logged in.

This code would be on the same domain, at the root, so there would not be any cross-domain issues for retrieving the session information, if that was your question? So basically this is external code, but (I think) it still resides "inside" the session...

Hope this is more clear! Tepp

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1906
  • Karma: +290/-11
  • web for all, all for web!
    • View Profile
    • AbanteCart
Thanks, I took a look at the php file and method you suggested, I'm afraid it's my turn to not be sure I understand :-)

I see a bunch of bits which look interesting in there (e.g. $this->request->get['customer_id']), but the problem is I don't know how my php code can instanciate the "$this". I am too much of a beginner with respect to MVC frameworks, so I am hoping to simply be able to "get the cookie / read it", perhaps using some premade AbanteCart functions, in order to know who is logged in.

This code would be on the same domain, at the root, so there would not be any cross-domain issues for retrieving the session information, if that was your question? So basically this is external code, but (I think) it still resides "inside" the session...

Hope this is more clear! Tepp

You can use global array $_GET for access to $this->request->get['customer_id'].
$this is an object created by class. You don need this.

Just look on function call there:

startStorefrontSession(
     $this->user->getId(),
     [
      'customer_id' => $this->request->get['customer_id'],
      'merchant_username' => $this->user->getUserName()
     ]
);

you can play with parameters  for Example startStorefrontSession(1, [ 'customer_id' => $some_your_id ];

hope this help
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

Offline teppyogi

  • Newbie
  • *
  • Posts: 6
  • Karma: +1/-0
    • View Profile
So I tried this, but the response is empty. Here is my test.php code:

Quote
<?php
echo "hello, the logged in customer_id is: " . $_GET['customer_id'] . ".";
?>

I believe this would, in fact, get the customer_id if it were a GET parameter in the URL, am I correct? What I would like to get is the customer_id which is in the session, like in other bits of code I see: $this->session->data['customer_id']

I feel I'm close, but I still don't get it...

Offline teppyogi

  • Newbie
  • *
  • Posts: 6
  • Karma: +1/-0
    • View Profile
Ok, I was able to figure it out by exploring some of the AbanteCart code... Here is what I did, not sure if it is a "best practice" or if things are ugly but so far it seems to work:
Quote
<?php
$root_path = dirname(__FILE__);
if (defined('IS_WINDOWS')) {
    $root_path = str_replace('\\', '/', $root_path);
}
define('DIR_ROOT', $root_path);

// HTTP
$dirname = rtrim(dirname($_SERVER['PHP_SELF']), '/.\\');
$dirname = strip_tags(html_entity_decode($dirname, ENT_QUOTES, 'UTF-8'));
define('HTTP_SERVER', 'http://'.$_SERVER['HTTP_HOST'].$dirname);
define('HTTP_ABANTECART', 'http://'.$_SERVER['HTTP_HOST'].trim($dirname, 'static_pages'));

// DIR
define('DIR_APP_SECTION', str_replace('\'', '/', realpath(dirname(__FILE__))).'/');
define('DIR_CORE', str_replace('\'', '/', realpath(dirname(__FILE__).'/../')).'/core/');
define('DIR_ABANTECART', str_replace('\'', '/', realpath(DIR_APP_SECTION.'../')).'/');

// Startup
//~ require_once(DIR_ABANTECART.'system/config.php');
//~ require_once(DIR_CORE.'helper/utils.php');
//~ require_once(DIR_CORE.'lib/session.php');

//For some reason, the above directories are not populated on my server, so I hard code them below:
require_once('/mydomain_path/system/config.php');
require_once('/mydomain_path/core/helper/utils.php');
require_once('/mydomain_path/core/lib/session.php');

$from_admin = false;

$session_id = '';
if (isset($_GET['mode']) && $_GET['mode'] == 'admin') {
    $from_admin = true;
}

foreach (array_keys($_COOKIE) as $key) {
    if ($from_admin === true && preg_match("/^AC_CP/", $key)) {
        $session_id = $key;
        break;
    }
    if ($from_admin !== true && preg_match("/^AC_SF/", $key)) {
        $session_id = $key;
        break;
    }
}

define('SESSION_ID', $session_id);

//try to start session.
$session = new ASession(SESSION_ID);

$customerId = 0;
if ((isset($_SESSION['user_id']) || isset($_SESSION['customer_id']))){
  $customerId = $session->data['customer_id'];
}
?>

<!DOCTYPE html>
<html>
<body>
<?php
echo "hello, the logged in customer_id is: " . $customerId . ".";
?>
</body>
</html>

(sorry, for some reason I cannot seem to post "code" in the forum)

 

Powered by SMFPacks Social Login Mod