You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过XMLRPC获取指定客户对应产品模板的指定数量定价(无需建销售订单)

解决方案:通过XMLRPC获取指定产品和数量的价格(无需创建销售订单)

我经常处理这类Odoo XMLRPC的价格查询需求,给你整理一个可行的方案——核心是利用Odoo内置的product.pricelist模型的价格计算方法,不需要创建销售订单就能直接获取对应价格。

核心原理

Odoo的价格列表系统已经封装了计算产品最终价格的逻辑,你可以直接调用product.pricelist模型的以下两个方法:

  • get_product_price: 返回最终计算后的价格(浮点数)
  • get_product_price_rule: 返回元组(最终价格, 对应的价格规则ID),适合需要了解价格来源的场景

这两个方法会自动应用该价格列表下的所有折扣规则(包括基于产品类别、数量阶梯等规则),完全匹配你的需求。

步骤说明

1. 准备必要参数

你需要提前准备这些信息:

  • product_template_id: 目标产品模板ID
  • quantity: 需要计算价格的产品数量
  • partner_id(可选): 客户ID,系统会自动使用该客户绑定的价格列表;如果已经知道价格列表ID,也可以直接传pricelist_id替代
  • uom_id(可选): 产品单位ID,默认使用产品的主单位

2. 示例代码(Python)

下面是完整的XMLRPC调用示例,包含从模板ID获取产品变体ID、获取客户价格列表、计算价格的全流程:

import xmlrpc.client

# 配置你的Odoo连接信息
ODOO_URL = "http://your-odoo-instance-url"
ODOO_DB = "your-database-name"
ODOO_USER = "your-username"
ODOO_PWD = "your-password"

# 初始化XMLRPC连接
common_proxy = xmlrpc.client.ServerProxy(f"{ODOO_URL}/xmlrpc/2/common")
user_uid = common_proxy.authenticate(ODOO_DB, ODOO_USER, ODOO_PWD, {})
models_proxy = xmlrpc.client.ServerProxy(f"{ODOO_URL}/xmlrpc/2/object")

# --------------------------
# 1. 从产品模板ID获取对应的产品变体ID
# --------------------------
target_template_id = 123  # 替换成你的产品模板ID
product_variant_ids = models_proxy.execute_kw(
    ODOO_DB, user_uid, ODOO_PWD,
    "product.product", "search",
    [[["product_tmpl_id", "=", target_template_id]]]
)
if not product_variant_ids:
    print("未找到该产品模板对应的变体,请检查ID是否正确")
    exit()
target_product_id = product_variant_ids[0]

# --------------------------
# 2. 获取客户绑定的价格列表ID(如果已知pricelist_id可跳过此步)
# --------------------------
target_partner_id = 456  # 替换成你的客户ID
partner_info = models_proxy.execute_kw(
    ODOO_DB, user_uid, ODOO_PWD,
    "res.partner", "read",
    [target_partner_id, ["property_product_pricelist"]]
)
target_pricelist_id = partner_info[0]["property_product_pricelist"][0]

# --------------------------
# 3. 计算指定数量的产品价格
# --------------------------
target_quantity = 10  # 替换成你要计算的数量

# 方法1:仅获取最终价格
final_price = models_proxy.execute_kw(
    ODOO_DB, user_uid, ODOO_PWD,
    "product.pricelist", "get_product_price",
    [target_pricelist_id, target_product_id, target_quantity, target_partner_id]
)
print(f"计算后的产品价格:{final_price}")

# 方法2:获取价格+对应的价格规则ID
price_with_rule = models_proxy.execute_kw(
    ODOO_DB, user_uid, ODOO_PWD,
    "product.pricelist", "get_product_price_rule",
    [target_pricelist_id, target_product_id, target_quantity, target_partner_id]
)
final_price, rule_id = price_with_rule
print(f"计算后的价格:{final_price},对应的价格规则ID:{rule_id}")

注意事项

  • 权限问题: 确保你使用的Odoo用户拥有product.pricelist、product.product、res.partner模型的读取权限,否则会返回权限错误
  • 多单位支持: 如果需要按特定单位计算价格,可以在调用方法时传入uom_id参数(第五个位置参数)
  • 日期范围规则: 如果你的价格列表有时间限制的规则,可以传入date参数(第六个位置参数)指定计算日期
  • 无客户场景: 如果不需要基于客户的价格列表,可以直接传入pricelist_id,并将partner_id设为False

内容的提问来源于stack exchange,提问作者M.E.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:19:27