覆盖和更改函数 Odoo 的中间

Override and Change the middle of a function Odoo

我想覆盖 _cart_update 模块中的 website_sale 函数。但是我想改变函数的中间(我想在函数代码的中间做点什么)。所以我尝试在我的自定义模块中重写该函数并将 website_sale 模块添加为依赖模块(我重写了该函数而没有添加任何 super() 调用)。我的函数获取函数调用。没问题。但是还有其他模块使用 super() 调用覆盖相同的函数。可悲的是,这些功能将不会执行。但我希望他们被处决。

我的工作有什么问题吗?还是有其他方法可以做到这一点?

谢谢。

你可以做一个猴子补丁:

from odoo.addons import website_sale  # so we can update the method in the class
# import any global variable that the method use directly from of website_sale
# I think I covered all variable but in version 11, I don't know if they change it in 12
from odoo.addons.website_sale.models.sale_order import _logger, ValidationError, request, UserError, _, api
# import any thing you need to use in your custom code if there is

@api.multi
def _cart_update(self, product_id=None, line_id=None, add_qty=0, set_qty=0, attributes=None, **kwargs):
    # put the full code here
    # you don't need to call super because it will be the last one to call
    # it's like we defined it in the website_sale 

# at the end replace the method in the class that was defined in the website_sale
website_sale.models.SaleOrder._cart_update = _cart_update

唯一的解决办法是替换class中的方法,因为如果你在sub classes之一中使用super原始方法将是 executed.