Override and Change the middle of a function Odoo



我想覆盖website_sale模块中名为_cart_update的函数。但是我想改变函数的中间(我想在函数代码的中间做一些事情(。因此,我尝试重写自定义模块中的函数,并添加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

唯一的解决方案是替换中的方法,因为如果在其中一个sub classes中使用super,则原始方法将是executed

最新更新