summaryrefslogtreecommitdiff
path: root/addons/website_sale_stock/static/src
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/website_sale_stock/static/src
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/website_sale_stock/static/src')
-rw-r--r--addons/website_sale_stock/static/src/js/variant_mixin.js93
-rw-r--r--addons/website_sale_stock/static/src/xml/website_sale_stock_product_availability.xml29
2 files changed, 122 insertions, 0 deletions
diff --git a/addons/website_sale_stock/static/src/js/variant_mixin.js b/addons/website_sale_stock/static/src/js/variant_mixin.js
new file mode 100644
index 00000000..0c201a82
--- /dev/null
+++ b/addons/website_sale_stock/static/src/js/variant_mixin.js
@@ -0,0 +1,93 @@
+odoo.define('website_sale_stock.VariantMixin', function (require) {
+'use strict';
+
+var VariantMixin = require('sale.VariantMixin');
+var publicWidget = require('web.public.widget');
+var ajax = require('web.ajax');
+var core = require('web.core');
+var QWeb = core.qweb;
+var xml_load = ajax.loadXML(
+ '/website_sale_stock/static/src/xml/website_sale_stock_product_availability.xml',
+ QWeb
+);
+
+/**
+ * Addition to the variant_mixin._onChangeCombination
+ *
+ * This will prevent the user from selecting a quantity that is not available in the
+ * stock for that product.
+ *
+ * It will also display various info/warning messages regarding the select product's stock.
+ *
+ * This behavior is only applied for the web shop (and not on the SO form)
+ * and only for the main product.
+ *
+ * @param {MouseEvent} ev
+ * @param {$.Element} $parent
+ * @param {Array} combination
+ */
+VariantMixin._onChangeCombinationStock = function (ev, $parent, combination) {
+ var product_id = 0;
+ // needed for list view of variants
+ if ($parent.find('input.product_id:checked').length) {
+ product_id = $parent.find('input.product_id:checked').val();
+ } else {
+ product_id = $parent.find('.product_id').val();
+ }
+ var isMainProduct = combination.product_id &&
+ ($parent.is('.js_main_product') || $parent.is('.main_product')) &&
+ combination.product_id === parseInt(product_id);
+
+ if (!this.isWebsite || !isMainProduct){
+ return;
+ }
+
+ var qty = $parent.find('input[name="add_qty"]').val();
+
+ $parent.find('#add_to_cart').removeClass('out_of_stock');
+ $parent.find('#buy_now').removeClass('out_of_stock');
+ if (combination.product_type === 'product' && _.contains(['always', 'threshold'], combination.inventory_availability)) {
+ combination.virtual_available -= parseInt(combination.cart_qty);
+ if (combination.virtual_available < 0) {
+ combination.virtual_available = 0;
+ }
+ // Handle case when manually write in input
+ if (qty > combination.virtual_available) {
+ var $input_add_qty = $parent.find('input[name="add_qty"]');
+ qty = combination.virtual_available || 1;
+ $input_add_qty.val(qty);
+ }
+ if (qty > combination.virtual_available
+ || combination.virtual_available < 1 || qty < 1) {
+ $parent.find('#add_to_cart').addClass('disabled out_of_stock');
+ $parent.find('#buy_now').addClass('disabled out_of_stock');
+ }
+ }
+
+ xml_load.then(function () {
+ $('.oe_website_sale')
+ .find('.availability_message_' + combination.product_template)
+ .remove();
+
+ var $message = $(QWeb.render(
+ 'website_sale_stock.product_availability',
+ combination
+ ));
+ $('div.availability_messages').html($message);
+ });
+};
+
+publicWidget.registry.WebsiteSale.include({
+ /**
+ * Adds the stock checking to the regular _onChangeCombination method
+ * @override
+ */
+ _onChangeCombination: function (){
+ this._super.apply(this, arguments);
+ VariantMixin._onChangeCombinationStock.apply(this, arguments);
+ }
+});
+
+return VariantMixin;
+
+});
diff --git a/addons/website_sale_stock/static/src/xml/website_sale_stock_product_availability.xml b/addons/website_sale_stock/static/src/xml/website_sale_stock_product_availability.xml
new file mode 100644
index 00000000..8bb6682e
--- /dev/null
+++ b/addons/website_sale_stock/static/src/xml/website_sale_stock_product_availability.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<templates>
+
+ <t t-name="website_sale_stock.product_availability">
+ <t t-if="product_type == 'product' and _.contains(['always', 'threshold'], inventory_availability)">
+ <t t-if="virtual_available gt 0">
+ <div t-if="inventory_availability == 'always'" t-attf-class="availability_message_#{product_template} text-success mt16">
+ <t t-esc="virtual_available_formatted" /> <t t-esc="uom_name" /> available
+ </div>
+ <t t-if="inventory_availability == 'threshold'">
+ <div t-if="virtual_available lte available_threshold" t-attf-class="availability_message_#{product_template} text-warning mt16">
+ <i class="fa fa-exclamation-triangle" title="Warning" role="img" aria-label="Warning"/>
+ <t t-esc="virtual_available_formatted" /> <t t-esc="uom_name" /> available
+ </div>
+ <div t-if="virtual_available gt available_threshold" t-attf-class="availability_message_#{product_template} text-success mt16">In stock</div>
+ </t>
+ </t>
+ <div t-if="cart_qty" t-attf-class="availability_message_#{product_template} text-warning mt8">
+ You already added <t t-if="!virtual_available">all</t> <t t-esc="cart_qty" /> <t t-esc="uom_name" /> in your cart.
+ </div>
+ <div t-if="!cart_qty and virtual_available lte 0" t-attf-class="availability_message_#{product_template} text-danger mt16"><i class="fa fa-exclamation-triangle" role="img" aria-label="Warning" title="Warning"/> Temporarily out of stock</div>
+ </t>
+ <div t-if="product_type == 'product' and inventory_availability == 'custom'" t-attf-class="availability_message_#{product_template} text-success mt16">
+ <t t-esc="custom_message" />
+ </div>
+ </t>
+
+</templates>