opencartopencart2.3

Get logged Username on Catalog Section (Opencart)


anyone know how to get logged username admin on catalog section? I've tried inject session, but not working. Already tried to get userID, still not working too. I need to add logged in username on addOrderHistory method. I'm using Opencart 2.3.0.2

Additional info: Opencart 2.3.0.2 call method addOrderHistory from controller api at catalog, and the problem is I can't get admin user id on catalog because it has separates session/function


Solution

  • By default, the admin User class is not instantiated on the front end, so there are a couple of options here.

    Option 1: Get the admin user_id directly from the session. This is the easiest thing to do if you only need to know the user_id and/or if the person is a logged in admin user. The admin's user_id is stored as a session variable and is globally accessible as $this->session->data['user_id']. Of course it may not exist so you'll need to check.

    if (isset($this->session->data['user_id'])) {
        $user_id = $this->session->data['user_id'];
    }
    

    Option 2: What you actually asked for is to get the username. To do this, you'll need to instantiate the User class and pass the $registry object as an argument to the constructor. If you are using PHP 5.4 or greater you can do this and call it's methods directly by wrapping the newly created object in parentheses as illustrated here:

    $username = (new Cart\User($this->registry))->getUsername();
    

    On earlier versions of PHP you can instantiate the class in the traditional way and call it's methods:

    $this->user = new Cart\User($this->registry);
    $username   = $this->user->getUsername();
    $user_id    = $this->user->getId();
    $user_group = $this->user->getGroupId();
    

    You can do this on the fly anywhere in a controller or model, but if you think you'll need frequent access to the User methods on the front end, the best place is in catalog/controller/startup/startup.php and register it with the registry class which makes it globally accessible:

    $this->registry->set('user', new Cart\User($this->registry));
    

    Then you can call it anytime, anywhere on the store front end like:

    $this->user->getUsername();
    

    One very important caveat here pertains to your desire to get this data from the addOrderHistory() method as mentioned in your question. You need to bear in mind that this method might be called as part of a callback by a number of payment methods — like Paypal Standard for instance. In this case, the method is not called by the customer's browser session and therefore even if it is an admin who is checking out it will not be able to access the admin's session. If you absolutely need to grab data about an admin who is checking out, you need to do this in the addOrder() method instead since that's always called directly from a browser session.

    UPDATE — for getting admin username through the api: If you are accessing front end controllers through the API, Opencart creates a new session using catalog/controller/api/login.php. Since this api session doesn't know anything about your admin session by default, you'll need to add your user_id there for the methods I described above to work. Look at that file and you will see a line that looks like this (which starts the api session):

    $this->session->start('api', $session_id_new);
    

    You will need to modify things to capture your user id. Something like this:

    // store user_id from admin session
    $user_id = $this->session->data['user_id'];
    // start the api session
    this->session->start('api', $session_id_new);
    // add admin user_id to your api session
    $this->session->data['user_id'] = $user_id;
    

    Now your admin user_id is part of the api session so you can proceed to instantiate the user class as described above.