Help

0

Assigning value to extended field when adding to cart in odoo ecommerce

Avatar
joozer


Now I am extending the new field (boolean) to the object, sale.order.

My default value of extended field is False.

Extending part is ok and I already added this field to tree, form view.

So my original idea is when user press "add to cart" button in webshop, my new extended field automatically assign true (boolean) value. In Odoo, adding cart is like creating new Quotation in back-end if Quotation haven't created yet.

Here is my extended field:

from odoo import api, fields, models

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

   is_value_over = fields.Boolean(string='value_over')

I just realized that I needed to assign this value in two main functions under website_sale module, as below:

 @http.route(['/shop/cart'], type='http', auth="public", website=True)
def cart(self, **post):

  @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):

How can I assign is_value_over value to true when user is adding item to the cart?


Avatar
Discard
2Answers
0
Avatar
Daniel Chuc
Best Answer

in my case

from odoo.addons.website_sale.controllers.main import WebsiteSale

class PresaleAndFinancing(WebsiteSale):

@http.route(['/shop/cart/update/presale'], type='json', auth="public", methods=['POST'], website=True)
def cart_update_multi_variant(self,data,**post):
data = data[0] # ignore
month_qty = data['presale'] + data['financing'] #i gnore
# update the cart (native method, create and validate the order if necessary)
self.cart_update(
product_id = data['product_id'], // if of product, check other values in my case not necesary
)
# get the obj of order actual
order = request.website.sale_get_order()
# update the new field created in sale.order
order.sudo().write({'month_qty': month_qty})
Avatar
Discard
0
Avatar
Paresh Wagh
Best Answer

If you change the class definition line in your extension code from

class add_to_cart_extend(models.Model):

to

class SaleOrder(models.Model):

the base SaleOrder class will be extended and the new field will be added to the sale.order model. You can then set its value just like all the other fields available in the model.

Avatar
Discard