flectra/addons/website_sale_options/controllers/main.py
2018-01-16 02:34:37 -08:00

83 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
from flectra import http
from flectra.http import request
from flectra.addons.website_sale.controllers.main import WebsiteSale
class WebsiteSaleOptions(WebsiteSale):
@http.route(['/shop/product/<model("product.template"):product>'], type='http', auth="public", website=True)
def product(self, product, category='', search='', **kwargs):
r = super(WebsiteSaleOptions, self).product(product, category, search, **kwargs)
r.qcontext['optional_product_ids'] = [p.with_context({'active_id': p.id}) for p in product.optional_product_ids]
return r
@http.route(['/shop/cart/update_option'], type='http', auth="public", methods=['POST'], website=True, multilang=False)
def cart_options_update_json(self, product_id, add_qty=1, set_qty=0, goto_shop=None, lang=None, **kw):
if lang:
request.website = request.website.with_context(lang=lang)
order = request.website.sale_get_order(force_create=1)
product = request.env['product.product'].browse(int(product_id))
option_ids = product.optional_product_ids.mapped('product_variant_ids').ids
optional_product_ids = []
for k, v in kw.items():
if "optional-product-" in k and int(kw.get(k.replace("product", "add"))) and int(v) in option_ids:
optional_product_ids.append(int(v))
attributes = self._filter_attributes(**kw)
value = {}
if add_qty or set_qty:
value = order._cart_update(
product_id=int(product_id),
add_qty=add_qty,
set_qty=set_qty,
attributes=attributes,
optional_product_ids=optional_product_ids
)
# options have all time the same quantity
for option_id in optional_product_ids:
order._cart_update(
product_id=option_id,
set_qty=value.get('quantity'),
attributes=attributes,
linked_line_id=value.get('line_id')
)
return str(order.cart_quantity)
@http.route(['/shop/modal'], type='json', auth="public", methods=['POST'], website=True)
def modal(self, product_id, **kw):
pricelist = request.website.get_current_pricelist()
product_context = dict(request.context)
quantity = kw['kwargs']['context']['quantity']
if not product_context.get('pricelist'):
product_context['pricelist'] = pricelist.id
# fetch quantity from custom context
product_context.update(kw.get('kwargs', {}).get('context', {}))
from_currency = request.env.user.company_id.currency_id
to_currency = pricelist.currency_id
compute_currency = lambda price: request.env['res.currency']._compute(from_currency, to_currency, price)
product = request.env['product.product'].with_context(product_context).browse(int(product_id))
main_product_attr_ids = self.get_attribute_value_ids(product)
for variant in main_product_attr_ids:
if variant[0] == product.id:
# We indeed need a list of lists (even with only 1 element)
main_product_attr_ids = [variant]
break
return request.env['ir.ui.view'].render_template("website_sale_options.modal", {
'product': product,
'quantity': quantity,
'compute_currency': compute_currency,
'get_attribute_value_ids': self.get_attribute_value_ids,
'main_product_attr_ids': main_product_attr_ids,
})