Search code examples
pythonodooe-commerceodoo-16odoo-website

Dynamic update of add_qty parameter in Odoo eCommerce product details page


I'm customizing an Odoo eCommerce website and I need to modify the add_qty parameter in the product details page dynamically via a URL parameter. I'm extending the WebsiteSale class to achieve this.

Here's my code snippet:

class WebsiteSaleCustom(WebsiteSale):
    def _prepare_product_values(self, product, category, search, **kwargs):
        values = super()._prepare_product_values(product, category, search, **kwargs)
        values['add_qty'] = int(kwargs.get('add_qty', 1))
        return values

This solution works as expected upon the first load of the product details page. However, the issue arises when attempting to dynamically update the add_qty parameter.

Suppose i hit this link

http://localhost:8069/shop/black-shoe?add_qty=5

Quantity will set 5. For first time, it is setting 5 but if i change quantity it not changing in quantity field. It's remaining 5.

Any insights or suggestions on how to achieve this would be greatly appreciated. Thank you for your help!


Solution

  • To address issues related to outdated or incorrect data being fetched by Odoo due to caching, you can use the method request.env.registry._clear_cache() before applying your solution. This method clears the cache, ensuring that Odoo fetches fresh data.

    from odoo.http import request
    from odoo.addons.website_sale.controllers.main import WebsiteSale
    
    
    class WebsiteSaleCustom(WebsiteSale):
        def _prepare_product_values(self, product, category, search, **kwargs):
            request.env.registry._clear_cache()
            values = super()._prepare_product_values(product, category, search, **kwargs)
            values['add_qty'] = int(kwargs.get('add_qty', 1))
            return values