[IMP] website_sale(_stock): simplify methods for cart

This commit simplifies the methods to add context values in
`website_sale_*` brigde modules.

task-3074645

Part-of: odoo/odoo#117379
This commit is contained in:
Valentin Vallaeys (vava)
2023-08-31 16:05:44 +00:00
parent fedecc1f27
commit 99257e681d
3 changed files with 25 additions and 39 deletions
@@ -27,21 +27,21 @@ export const cartHandlerMixin = {
/**
* @private
*/
_addToCartInPage(params) {
async _addToCartInPage(params) {
params.force_create = true;
return this._rpc({
const data = await this._rpc({
route: "/shop/cart/update_json",
params: params,
}).then(async data => {
sessionStorage.setItem('website_sale_cart_quantity', data.cart_quantity);
if (data.cart_quantity && (data.cart_quantity !== parseInt($(".my_cart_quantity").text()))) {
// No animation if the product's page images are hidden
if ($('div[data-image_width]').data('image_width') !== 'none') {
await animateClone($('header .o_wsale_my_cart').first(), this.$itemImgContainer, 25, 40);
}
updateCartNavBar(data);
})
sessionStorage.setItem('website_sale_cart_quantity', data.cart_quantity);
if (data.cart_quantity && (data.cart_quantity !== parseInt($(".my_cart_quantity").text()))) {
// No animation if the product's page images are hidden
if ($('div[data-image_width]').data('image_width') !== 'none') {
await animateClone($('header .o_wsale_my_cart').first(), this.$itemImgContainer, 25, 40);
}
});
updateCartNavBar(data);
};
return data;
},
};
+2 -1
View File
@@ -1618,7 +1618,7 @@
</div>
<t t-set="redirect_to_sign_in" t-value="website.account_on_checkout == 'mandatory' and website.is_public_user()"/>
<t t-set="redirect_url" t-value="'/web/login?redirect=/shop/checkout' if redirect_to_sign_in else '/shop/checkout?express=1'"/>
<div class="d-flex flex-column"
<div id="process_checkout" class="d-flex flex-column"
t-if="website_sale_order and website_sale_order.website_order_line">
<a class="btn btn-primary float-end" role="button" t-attf-href="{{redirect_url}}">
<t t-if="redirect_to_sign_in">
@@ -2164,6 +2164,7 @@
<t t-set="redirect_url" t-value="'/web/login?redirect=/shop/checkout' if redirect_to_sign_in else '/shop/checkout?express=1'"/>
<a role="button" t-if="website_sale_order and website_sale_order.website_order_line"
class="btn btn-secondary float-end d-none d-xl-inline-block"
id="process_checkout_cart_summary"
t-attf-href="{{redirect_url}}">
<t t-if="redirect_to_sign_in">
<span>Sign In</span>
+12 -27
View File
@@ -84,33 +84,18 @@ class SaleOrder(models.Model):
)
return self.shop_warning
def _get_cache_key_for_line(self, line):
return line.product_id
def _get_context_for_line(self, line):
return {
'website_sale_stock_get_quantity': True,
}
def _filter_can_send_abandoned_cart_mail(self):
""" Filter sale orders on their product availability. """
self = super()._filter_can_send_abandoned_cart_mail()
combination_info_cache = {}
return super()._filter_can_send_abandoned_cart_mail().filtered(
lambda so: so._all_product_available()
)
def _are_all_product_available_for_purchase(sale_order):
for line in sale_order.order_line:
product = line.product_id
if product.type != 'product':
continue
cache_key = self._get_cache_key_for_line(line)
combination_info = combination_info_cache.get(cache_key)
if not combination_info:
combination_info = product.with_context(**self._get_context_for_line(line))._get_combination_info_variant(add_qty=line.product_uom_qty)
combination_info_cache[cache_key] = combination_info
if not product.allow_out_of_stock_order and combination_info['free_qty'] == 0:
return False
return True
# If none of the products in the checkout are available for purchase (empty inventory, for example),
# then the email won't be sent.
return self.filtered(_are_all_product_available_for_purchase)
def _all_product_available(self):
for line in self.with_context(website_sale_stock_get_quantity=True).order_line:
product = line.product_id
if product.type != 'product' or product.allow_out_of_stock_order:
continue
combination_info = product._get_combination_info_variant(add_qty=line.product_uom_qty)
if combination_info['free_qty'] == 0:
return False
return True