Ueb Posted September 16, 2016 Share Posted September 16, 2016 Hi there, I need to log viewed products by user. I've found that PrestaShop stores searched keywords in table ps_statssearch, but where does it store viewed products? Link to comment Share on other sites More sharing options...
rocky Posted September 17, 2016 Share Posted September 17, 2016 The customer's viewed products are stored in the viewed variable in the customer's cookie, not in the database. Link to comment Share on other sites More sharing options...
Ueb Posted September 20, 2016 Author Share Posted September 20, 2016 Ok thank you @Ambassadors. What do you suggest to store viewed products on DB ? Do I need to modify the SearchController ? I'd like to store (date, product_id, user_id) Link to comment Share on other sites More sharing options...
rocky Posted September 21, 2016 Share Posted September 21, 2016 You'll need to create a ps_viewed_products table in your database using an SQL query like the following on the SQL tab in phpMyAdmin: CREATE TABLE IF NOT EXISTS `ps_viewed_products` ( `id_customer` int(11) UNSIGNED NOT NULL, `id_product` int(11) UNSIGNED NOT NULL, `date` datetime NOT NULL, PRIMARY KEY (`id_customer`, `id_product`, `date`) ) ENGINE = InnoDB DEFAULT CHARSET=utf8 Then you'll need to create override/controllers/front/ProductController.php with the following: <?php class ProductController extends ProductControllerCore { public function initContent() { parent::initContent(); if ((int)$this->context->customer->id && Validate::isLoadedObject($this->product)) { Db::getInstance()->Execute( 'INSERT INTO `'._DB_PREFIX_.'viewed_products` (`id_customer`, `id_product`, `date`) VALUES ('. (int)$this->context->customer->id.', '.(int)$this->product->id.', "'.date('Y-m-d H:i:s').'")' ); } } } Remember to go to the Advanced Parameters > Performance tab in the Back Office and click the "Clear cache" button (or manually delete cache/class_index.php) so PrestaShop can find the override. Note that my code above assumes you want the date recorded every time a customer visits a product, even if they've visited it before. If you want only the last time a customer visited a product recorded, you'll need to modify it to check whether the customer has already visited the product and update the date instead of adding a new row. Link to comment Share on other sites More sharing options...
Ueb Posted September 22, 2016 Author Share Posted September 22, 2016 Thank you @Ambassadors, it works! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now