AnJ*_*AnJ 0 javascript python e-commerce odoo odoo-13
我有一个创建表单的自定义模块。根据此表格中的答案,我正在生成订单行。用户发送此表单后,我正在使用生成的订单行中的所有产品创建销售订单。
因此,我从 JavaScript 发送了一个包含要购买的产品的 JSON:
order_data = [{product_id: 1, amount: 10, …},{product_id: 2, …}, …];
note = '';
this._rpc({
route: '/api/create_order',
params: { order_products: order_data, note: note }
}).then((data) => {
window.location = '/contactus-thank-you';
}).catch((error) => {
console.error(error);
});
Run Code Online (Sandbox Code Playgroud)
然后在 Python 中,我根据 JSON 创建销售订单:
@http.route('/api/create_order', type='json', auth='user', website=True)
def create_order(self, **kw):
uid = http.request.env.context.get('uid')
partner_id = http.request.env['res.users'].search([('id','=',uid)]).partner_id.id
order_products = kw.get('order_products', [])
note = kw.get('note', '')
order_line = []
for product in order_products:
amount = 0
if 'custom_amount' in product:
amount = product['custom_amount']
else:
amount = product['amount']
if amount > 0:
order_line.append(
(0, 0, {
'product_id': product['product_id'],
'product_uom_qty': amount,
}))
order_data = {
'name': http.request.env['ir.sequence'].with_user(SUPERUSER_ID).next_by_code('sale.order') or _('New'),
'partner_id': partner_id,
'order_line': order_line,
'note': note,
}
result_insert_record = http.request.env['sale.order'].with_user(SUPERUSER_ID).create(order_data)
return result_insert_record.id
Run Code Online (Sandbox Code Playgroud)
但不是直接生成销售订单,我需要使用来自 Odoo 电子商务插件的工作流。这样用户可以例如编辑送货地址,选择付款等。所以我想我只需要以编程方式将所有产品放入购物车,然后剩下的将由 Odoo 内置功能处理。但是如何?我试图在 Odoo 源代码中找到一些东西,但很难掌握任何东西。
Odoo 使用典型的销售订单来处理购物车内的产品。但是这个过程并不像仅仅用一些产品创建销售订单那么简单。Odoo 需要知道哪个订单与哪个购物车等相关联。
幸运的是,Odoo 有处理它的方法。有一种sale_get_order()
方法可以让您获取当前与购物车链接的订单,或者在没有订单时创建新订单。
我不确定它是否被记录在源代码之外的任何地方,所以这里是代码 ( /addons/website_sale/models/website.py
) 中的一个片段:
def sale_get_order(self, force_create=False, code=None, update_pricelist=False, force_pricelist=False):
""" Return the current sales order after mofications specified by params.
:param bool force_create: Create sales order if not already existing
:param str code: Code to force a pricelist (promo code)
If empty, it's a special case to reset the pricelist with the first available else the default.
:param bool update_pricelist: Force to recompute all the lines from sales order to adapt the price with the current pricelist.
:param int force_pricelist: pricelist_id - if set, we change the pricelist with this one
:returns: browse record for the current sales order
"""
# ...
Run Code Online (Sandbox Code Playgroud)
我将它与另一种方法一起使用_cart_update()
,使我可以轻松更新此订单内的产品。还有sale_reset()
并且我正在使用它只是为了确保每次都会使用特定的销售订单更新当前会话。
sale_order = request.website.sale_get_order(force_create=True)
request.website.sale_reset()
sale_order.write({'order_line':[(5, 0, 0)]})
for product in order_products:
sale_order._cart_update(product_id=product['product_id'], line_id=None, add_qty=None, set_qty=product['amount'])
Run Code Online (Sandbox Code Playgroud)