Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 911
devbull Avatar asked Feb 10 '23 22:02

devbull


2 Answers

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.

like image 100
elembivos Avatar answered Apr 28 '23 12:04

elembivos


  • 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
like image 38
Abdo Adel Avatar answered Apr 28 '23 13:04

Abdo Adel