Files
odoo_source/addons/website_sale/models/website_visitor.py
T
fja-odoo e33172e832 [IMP] website_sale, *: keep track of products viewed
*= website, website_livechat, website_rating

///// Tracking Product /////

Now when a user browse products in eCommerce, we keep track of the
products he looked at. We use the website_visitor
to store the products viewed. A cookie is added with a TTl of 30 min it
will prevent the RPC for that time. We track the page only if the
product view is tracked.

The recently viewed products are displayed as a snippet but also
with the customize option in product pages of website_sale.

Products that are in cart will not be returned as recently viewed.

It is possible to add a recently viewed product to the cart directly
from the carousel, it will not redirect to the cart. If we are on the
cart page, the product is displayed in the cart.

The Visitor page in website now references products viewed

///// Tracking Page /////

Feature to track a view was remove in: https://github.com/odoo/enterprise/pull/4834

That feature is now reintroduced and will use website_track instead of
leads to be stored.

The track field is now on the view instead of the page.

url field is added to website.track, it will store the url for pages and
views

The Visitor page in website now references urls viewed

Add some tests

task-1984575

closes odoo/odoo#35810

Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
2019-09-16 09:38:53 +00:00

46 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime, timedelta
from odoo import fields, models, api
class WebsiteTrack(models.Model):
_inherit = 'website.track'
product_id = fields.Many2one('product.product', index=True, ondelete='cascade', readonly=True)
class WebsiteVisitor(models.Model):
_inherit = 'website.visitor'
visitor_product_count = fields.Integer('Product Views', compute="_compute_product_statistics", help="Total number of views on products")
product_ids = fields.Many2many('product.product', string="Visited Products", compute="_compute_product_statistics")
product_count = fields.Integer('Products Views', compute="_compute_product_statistics", help="Total number of product viewed")
@api.depends('website_track_ids')
def _compute_product_statistics(self):
results = self.env['website.track'].read_group(
[('visitor_id', 'in', self.ids), ('product_id', '!=', False)], ['visitor_id', 'product_id'], ['visitor_id', 'product_id'], lazy=False)
mapped_data = {}
for result in results:
visitor_info = mapped_data.get(result['visitor_id'][0], {'product_count': 0, 'product_ids': set()})
visitor_info['product_count'] += result['__count']
visitor_info['product_ids'].add(result['product_id'][0])
mapped_data[result['visitor_id'][0]] = visitor_info
for visitor in self:
visitor_info = mapped_data.get(visitor.id, {'product_ids': [], 'product_count': 0})
visitor.product_ids = [(6, 0, visitor_info['product_ids'])]
visitor.visitor_product_count = visitor_info['product_count']
visitor.product_count = len(visitor_info['product_ids'])
def _add_viewed_product(self, product_id):
""" add a website_track with a page marked as viewed"""
self.ensure_one()
if product_id and self.env['product.product'].browse(product_id)._is_variant_possible():
domain = [('product_id', '=', product_id)]
website_track_values = {'product_id': product_id, 'visit_datetime': datetime.now()}
self._add_tracking(domain, website_track_values)