devbull
devbull

Reputation: 227

Opencart 2 : Add currently logged admin id to oc_product table on product insert

I would like to monitor which dashboard user ("admin") added new product to the database.

The solution i was thinking about is simply adding another insert under admin > model > catalog > product.tpl under function addProduct(), which adds the user id to the custom column added before under oc_product.

$userID = // currently logged in

public function addProduct($data) {

$this->event->trigger('pre.admin.product.add', $data);

$this->db->query("INSERT INTO " . DB_PREFIX . "product SET addedby = $userID, .......");

.......

}

The only problem I have now is how to call / get the currently logged admin id inside this file (model/catalog/product.tpl).

This is just how i think about it, if this idea is completely wrong, please do write some better solutions.

Upvotes: 4

Views: 1837

Answers (2)

elembivos
elembivos

Reputation: 399

It would be better if you create another table to store this information since it will save you from altering the core table. In new table you store the user_id and product_id and set product_id as primary key. Now you will be able to fetch this data as you require by joining these two tables ON basis of product_id match.

Upvotes: 5

Abdelrhman Adel
Abdelrhman Adel

Reputation: 1187

  • The idea is correct (at least for me, this is how I was going to do it)
  • You can get the id of the currently logged-in admin through a call to $this->user->getId()
  • Add this code fragment $userID = $this->user->getId() inside the addProduct function, not inside the class declaration
  • There is no such a column named added_by in product table, you will have to alter the table structure and add it

Upvotes: 3

Related Questions