Jump to content

Product search log with user


Ueb

Recommended Posts

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...