Method to get product price for a given customer

浪子不回头ぞ 提交于 2021-01-07 03:46:23

问题


I need to retrieve product price via XMLRPC.

I am using Product Price Lists so each customer can be assigned a given price list which gives specific discounts based on categories, etc.

I am struggling to find which method can be used to retrieve the price for a given product_template id at a given quantity, if that is actually possible.

So far I have not been able to try any specific method as I can not identify how can this be achieved without actually creating a sales order.


回答1:


The module 'product' holds the pricelist mechanics. The model product.pricelist has a really nice method get_product_price(), which could be easily used server-side but not for the external/web API.

But if you have the possibility to write a little custom module, do that and override the model product.pricelist. Add the possibility to use this method, like:

Origin Method which can't be used because parameters are RecordSets:

def get_product_price(self, product, quantity, partner, date=False, uom_id=False):
    """ For a given pricelist, return price for a given product """
    self.ensure_one()
    return self._compute_price_rule([(product, quantity, partner)], date=date, uom_id=uom_id)[product.id][0]

"Wrapper" for external/web API:

def web_api_get_product_price(
    self, product_id, quantity, partner_id, date=False, uom_id=False):
    """ For a given pricelist, return price for a given product 
        callable from web api"""
    self.ensure_one()
    # get records
    product = self.env['product.product'].browse(product_id)
    partner = self.env['res.partner'].browse(partner_id)
    # call origin method
    return self.get_product_price(
        product, quantity, partner, date=date, uom_id=uom_id)

Now you can call this method, an example:

import xmlrpclib
db = 'db_name'
password = 'admin'
common = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/2/common')
uid = common.authenticate(db, 'admin', password, {})
models = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/2/object')
pricelist_id = 1
product_id = 5
partner_id = 7
quantity = 20
price = models.execute_kw(
    db, uid, password, 'product.pricelist',
    'web_api_get_product_price',
    [[pricelist_id], product_id, quantity, partner_id], {})


来源:https://stackoverflow.com/questions/49684565/method-to-get-product-price-for-a-given-customer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!