Post by broopenc3 » Sat Oct 13, 2018 1:00 am

Hello,

as the title suggests, I am trying to get the firstname (or maybe the lastname and username also from the user name), so that I can add it to a custom query in order_history.

What I have made so far (maybe a little irrelevant from what I am asking):
in the file /catalog/model/checkout/order.php

Code: Select all

//line 345 aproximatly 
$name_for_added_by="admin";

$this->db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '" . (int)$notify . "', added_by = '".$name_for_added_by."', comment = '" . $this->db->escape($comment) . "', date_added = NOW()");
with this in mind I can see in my SQL that the field "added_by" in the table " . DB_PREFIX . "order_history is correctly populated with the word "admin";

Now what i am trying to do is set "$name_for_added_by" with the user that presses the button-history (to update the history);
What I found (from /admin/controller/common/header.php) is that I need "$user_info['firstname']". However I can not get something to work.

Any ideas? :) Thanx

New member

Posts

Joined
Fri Mar 16, 2018 7:09 pm

Post by DigitCart » Sat Oct 13, 2018 2:00 am

Hi,
In your controller file, use it:

Code: Select all

$user_name =  $this->user->getUserName();

//echo $user_name;

$this->load->model('user/user');
$user_info = $this->model_user_user->getUser($this->user->getId());

//print_r($user_info);

My Extensions


User avatar
Active Member

Posts

Joined
Thu Jun 22, 2017 5:32 pm


Post by straightlight » Sat Oct 13, 2018 4:22 am

Better to use the user ID than the username on the database for tracking purposes.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by broopenc3 » Sat Oct 13, 2018 3:52 pm

DigitCart wrote:
Sat Oct 13, 2018 2:00 am
Hi,
In your controller file, use it:
Thanx for the answer! I managed to make it work in the header and any variabe I want from the user is accesible but I can not pass any variable in the 'public_html/catalog/model/checkout/order.php' file. So, final question: where is the controller file that I need to edit?

(ps: order history edit in opencart3 is different from opencart 1.5)

New member

Posts

Joined
Fri Mar 16, 2018 7:09 pm

Post by broopenc3 » Sat Oct 13, 2018 3:54 pm

straightlight wrote:
Sat Oct 13, 2018 4:22 am
Better to use the user ID than the username on the database for tracking purposes.
You are right and thank you for the suggestion. However for the purposes I need and the current situation, the username is enough.

New member

Posts

Joined
Fri Mar 16, 2018 7:09 pm

Post by broopenc3 » Sat Oct 13, 2018 7:04 pm

Update: I edited file "admin/controller/common/header.php"

Code: Select all

	public function index() {

		$user_name =  $this->user->getUserName();
		$this->load->model('user/user');
		$user_info = $this->model_user_user->getUser($this->user->getId());

		$GLOBALS["name_for_added_by"] = $user_name ; //either this
		$GLOBALS["name_for_added_by"] = $user_info['username']; //or this
but the variable still did not pass the value to the query in "catalog/model/checkout/order.php"

Code: Select all

$this->db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '" . (int)$notify . "', added_by = '".$GLOBALS["name_for_added_by"]."', comment = '" . $this->db->escape($comment) . "', date_added = NOW()");

New member

Posts

Joined
Fri Mar 16, 2018 7:09 pm

Post by straightlight » Sat Oct 13, 2018 9:42 pm

$GLOBALS is disabled by default in OC from the system folder for security reasons. A parameter needs to be passed with the username which is why I indicated above that the user ID would be more relative since a similar method already adds a user into the database as you demonstrate already. You just need to duplicate that method for your own database table target name.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by broopenc3 » Mon Oct 15, 2018 8:59 pm

straightlight wrote:
Sat Oct 13, 2018 9:42 pm
$GLOBALS is disabled by default in OC from the system folder for security reasons. A parameter needs to be passed with the username which is why I indicated above that the user ID would be more relative since a similar method already adds a user into the database as you demonstrate already. You just need to duplicate that method for your own database table target name.
allright got it... however how do i get the current user id?
neither: $this->session->data['user_id']
nor: $this->user->getId()
are working. :/

New member

Posts

Joined
Fri Mar 16, 2018 7:09 pm

Post by DigitCart » Mon Oct 15, 2018 9:22 pm

Hi,
admin and catalog are separate. if you want to get the user ID of admin in catalog (front-end), in your controller file use:

Code: Select all

$this->user = new Cart\User($this->registry);
$user_id = $this->user->getId(); // Returns null  or user ID

My Extensions


User avatar
Active Member

Posts

Joined
Thu Jun 22, 2017 5:32 pm


Post by broopenc3 » Mon Oct 15, 2018 9:39 pm

DigitCart wrote:
Mon Oct 15, 2018 9:22 pm
Hi,
admin and catalog are separate. if you want to get the user ID of admin in catalog (front-end), in your controller file use:

Code: Select all

$this->user = new Cart\User($this->registry);
$user_id = $this->user->getId(); // Returns null  or user ID
I tried in the controller/common/header.php with no results, unless you mean in system/engine/controller.php which throws an error.

I made a duplicate of model/user/user.php which loads in the order.php. Could I get the user_id there??? Like:

Code: Select all

<?php class ModelUserUser extends Model {
	public function getUserName() {
		$query = $this->db->query("SELECT *, (SELECT ug.name FROM `" . DB_PREFIX . "user_group` ug WHERE ug.user_group_id = u.user_group_id) AS user_group FROM `" . DB_PREFIX . "user` u WHERE u.user_id = '" . $user_id_HERE . "'");
		return $query->row;
	}
}

New member

Posts

Joined
Fri Mar 16, 2018 7:09 pm

Post by DigitCart » Mon Oct 15, 2018 9:57 pm

I tried in the controller/common/header.php
I just tested in header.php and it worked. make sure you've cleared caches.

My Extensions


User avatar
Active Member

Posts

Joined
Thu Jun 22, 2017 5:32 pm


Post by broopenc3 » Tue Oct 16, 2018 2:36 pm

DigitCart wrote:
Mon Oct 15, 2018 9:57 pm
I tried in the controller/common/header.php
I just tested in header.php and it worked. make sure you've cleared caches.
:/ I generaly have deactivated caching for admin menu. I cleared it once more but did not work. However I will look into it further just in case there is a problem.

New member

Posts

Joined
Fri Mar 16, 2018 7:09 pm

Post by rale » Sun May 17, 2020 6:35 am

How can show admin name on invoice? oc 3020

User avatar
New member

Posts

Joined
Sun Apr 17, 2016 1:44 am

Post by straightlight » Sun May 17, 2020 9:57 am

rale wrote:
Sun May 17, 2020 6:35 am
How can show admin name on invoice? oc 3020
Unless using an extension, the core does not provide an admin ID with the order. However, it could be done if you're looking to show the current session of the admin name once the invoice being created from the admin.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 111 guests