summaryrefslogtreecommitdiff
path: root/addons/website_sale_stock/static/src/js
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/js
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/website_sale_stock/static/src/js')
-rw-r--r--addons/website_sale_stock/static/src/js/variant_mixin.js93
1 files changed, 93 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;
+
+});