Skip to Content
Menu
This question has been flagged
1 Reply
1912 Views

Hi,


I'm able to add my custom form elements to cart like the screenshot below: 

https://pasteboard.co/ikyZ5uPfsj1y.png


My question is: How to override the original product price (adding it manually) and display the new price in the cart using the above screenshot price field?

Avatar
Discard
Best Answer

Hi,

Create a controller for the button submit

do this in the controller

order = request.website.sale_get_order(force_create=True)
values = order._cart_update(
product_id=product_id,
add_qty=add_qty,
set_qty=set_qty,
unit_price=unit_price,
)

now inherit sale.order and add the code below

def _prepare_order_line_update_values(
self, order_line, quantity, linked_line_id=False, **kwargs
):

values = super()._prepare_order_line_update_values(order_line, quantity, linked_line_id, **kwargs)
if 'unit_price' in kwargs:
values["unit_price"] = kwargs.get('unit_price')
return values

Regards

Avatar
Discard
Author

Hi,

After inheriting and adding your code in WebsiteSale controller , I get the following error message: name 'unit_price' is not defined. I can't figure what is wrong. Below is the code:

class WebsiteSaleInherit(WebsiteSale):
@http.route(['/shop/cart/update'], type='http', auth="public", methods=['POST'], website=True, csrf=False)
def cart_update(self, product_id, add_qty=1, set_qty=0, **kw):
res = super(WebsiteSaleInherit, self).cart_update(product_id, add_qty=1, set_qty=0, **kw)
order = request.website.sale_get_order(force_create=True)
values = order._cart_update(
product_id=product_id,
add_qty=add_qty,
set_qty=set_qty,
unit_price=unit_price,
)
return res

class SaleOrder(models.Model):
_inherit = 'sale.order'

def _prepare_order_line_update_values(self, order_line, quantity, linked_line_id=False, **kwargs):
values = super()._prepare_order_line_update_values(order_line, quantity, linked_line_id, **kwargs)
if 'unit_price' in kwargs:
values["unit_price"] = kwargs.get('unit_price')
return values

Thanks for helping out.

Hi Cybrosys,
I'm using Odoo v17.
I have a problem after updating the cart amount. When I go to check out and confirm order the price is recalculated by the function "_compute_price". How can I override this function with my customized price? I would appreciate any help from you.

Related Posts Replies Views Activity
1
Apr 23
2394
3
May 19
6144
0
Oct 22
659
0
Oct 22
379
1
Jul 20
4236