Odoo 18继承POS自助点餐商品卡片模板:自定义字段不显示
Odoo 18 POS自助点餐自定义字段无法通过模块继承模板显示的解决方案
问题排查与修复步骤
1. 修正前端资产包配置
你的manifest中错误使用了point_of_sale.assets作为资产目标,但POS自助点餐的前端资源属于pos_self_order模块的专属资产包,需调整为:
'assets': { 'pos_self_order.assets': [ 'self_order_customisation/static/src/xml/pos_product_card.xml', ], },
2. 优化XPath匹配表达式
原XPath可能因模板结构细节匹配失败,简化表达式确保精准定位:
<?xml version="1.0" encoding="UTF-8"?> <templates id="template" xml:space="preserve"> <t t-inherit="pos_self_order.ProductCard" t-inherit-mode="extension"> <xpath expr="//div[hasclass('product-infos')]" position="inside"> <t t-if="props.product.short_description"> <span class="mt-2 text-sm text-gray-600"><t t-esc="props.product.short_description"/></span> </t> <t t-if="props.product.preparation_time"> <span class="mt-1 text-xs text-gray-500">准备时间: <t t-esc="props.product.preparation_time"/></span> </t> </xpath> </t> </templates>
该写法直接将自定义内容插入到product-infos容器末尾,避免因名称元素的DOM结构变化导致匹配失效。
3. 确认字段同步到前端
若字段仍未显示,需验证前端是否接收到字段数据:
- 打开浏览器开发者工具(F12),切换到Network标签
- 加载POS自助点餐页面,找到产品数据请求(通常为
/pos_self_order/get_product_data) - 检查返回的product对象是否包含
short_description和preparation_time字段
如果未包含,可替换Python中的字段加载方法(Odoo 18推荐使用_pos_access_fields):
class ProductProduct(models.Model): _inherit = 'product.product' preparation_time = fields.Char(related='product_tmpl_id.preparation_time', store=True) short_description = fields.Char(related='product_tmpl_id.short_description', store=True) @api.model def _pos_access_fields(self): return super()._pos_access_fields() + ['short_description', 'preparation_time']
4. 强制刷新缓存与升级模块
- 在Odoo后台找到自定义模块,点击升级
- 按
Ctrl+Shift+R强制清除浏览器缓存,重新加载页面 - 开发者模式下点击清除缓存并重新加载按钮
验证
完成以上操作后,查看商品卡片即可看到自定义字段正常显示。直接修改源码能生效说明字段逻辑无问题,核心问题集中在资产加载路径和模板继承的匹配精度上。
内容的提问来源于stack exchange,提问作者NIDAL
相关产品推荐
相关产品推荐

