| 1 |
|
| 2 |
|
| 3 |
from odoo import _ |
| 4 |
from odoo.http import request |
| 5 |
|
| 6 |
from odoo.addons.website_sale.controllers.main import WebsiteSale |
| 7 |
|
| 8 |
|
| 9 |
class WebsiteSaleCollect(WebsiteSale): |
| 10 |
|
| 11 |
def _prepare_product_values(self, product, category, **kwargs): |
| 12 |
""" Override of `website_sale` to configure the Click & Collect Availability widget. """ |
| 13 |
res = super()._prepare_product_values(product, category, **kwargs) |
| 14 |
if in_store_dm_sudo := request.website.sudo().in_store_dm_id: |
| 15 |
order_sudo = request.cart |
| 16 |
selected_location_data = {} |
| 17 |
single_location = len(in_store_dm_sudo.warehouse_ids) == 1 |
| 18 |
if ( |
| 19 |
order_sudo.carrier_id.delivery_type == 'in_store' |
| 20 |
and order_sudo.pickup_location_data |
| 21 |
): |
| 22 |
selected_location_data = order_sudo.pickup_location_data |
| 23 |
elif single_location: |
| 24 |
selected_location_data = ( |
| 25 |
in_store_dm_sudo.warehouse_ids[0]._prepare_pickup_location_data() |
| 26 |
) |
| 27 |
res.update({ |
| 28 |
'selected_location_data': selected_location_data, |
| 29 |
'show_select_store_button': not single_location, |
| 30 |
'zip_code': ( |
| 31 |
order_sudo.partner_shipping_id.zip |
| 32 |
or selected_location_data.get('zip_code') |
| 33 |
or request.geoip.postal.code |
| 34 |
or '' |
| 35 |
), |
| 36 |
}) |
| 37 |
return res |
| 38 |
|
| 39 |
def _prepare_checkout_page_values(self, order_sudo, **query_params): |
| 40 |
""" Override of `website_sale` to include the unavailable products for the selected pickup |
| 41 |
location and set the pickup location when there is only one warehouse available. """ |
| 42 |
res = super()._prepare_checkout_page_values(order_sudo, **query_params) |
| 43 |
|
| 44 |
if order_sudo.only_services: |
| 45 |
return res |
| 46 |
|
| 47 |
res.update(order_sudo._prepare_in_store_default_location_data()) |
| 48 |
if order_sudo.carrier_id.delivery_type == 'in_store' and order_sudo.pickup_location_data: |
| 49 |
res['insufficient_stock_data'] = order_sudo._get_insufficient_stock_data( |
| 50 |
order_sudo.pickup_location_data.get('id') |
| 51 |
) |
| 52 |
return res |
| 53 |
|
| 54 |
def _get_shop_payment_errors(self, order): |
| 55 |
""" Override of `website_sale` to includes errors if no pickup location is selected or some |
| 56 |
products are unavailable. """ |
| 57 |
errors = super()._get_shop_payment_errors(order) |
| 58 |
if order._has_deliverable_products() and order.carrier_id.delivery_type == 'in_store': |
| 59 |
if not order.pickup_location_data: |
| 60 |
errors.append(( |
| 61 |
_("Sorry, we are unable to ship your order."), |
| 62 |
_("Please choose a store to collect your order."), |
| 63 |
)) |
| 64 |
else: |
| 65 |
selected_wh_id = order.pickup_location_data['id'] |
| 66 |
if not order._is_in_stock(selected_wh_id): |
| 67 |
errors.append(( |
| 68 |
_("Sorry, we are unable to ship your order."), |
| 69 |
_("Some products are not available in the selected store."), |
| 70 |
)) |
| 71 |
return errors |