skip to Main Content

I have this code that restricts the sale of the products from POS if they are out of stock. But it works when i select the product and then click on payment button. When I click on payment button on POS Session it will display a message for out of stock products. I want to add another feature, I want this message to be displayed when I click on out of stock products. For example, if a product is out of stock, when I click on the product it should give me the warning message. Out of stock products cannot be selected. So instead of selecting the out of stock products and then displaying the message while payment, I want to display the message when the product is selected. Can anyone help me to make the code work for odoo 17. I am new to Odoo POS development. Please help me out, Thank you.

    patch(Order.prototype, {
    async pay() {
        let order = this.env.services.pos.get_order();
        let lines = order.get_orderlines();
        let pos_config = this.env.services.pos.config;
        let config_id = this.env.services.pos.config.id;
        let prod_used_qty = {};
        let outOfStockProducts = []; 
        let restrict = false;
        
        if (pos_config.restrict_zero_qty) {
            lines.forEach(line => {
                let prd = line.product;
                if (prd.type === 'product') {
                    if (prd.id in prod_used_qty) {
                        let old_qty = prod_used_qty[prd.id][1];
                        prod_used_qty[prd.id] = [prd.qty_available, line.quantity + old_qty];
                    } else {
                        prod_used_qty[prd.id] = [prd.qty_available, line.quantity];
                    }

                    // Check if product is out of stock
                    if (prd.qty_available <= 0) {
                        restrict = true;
                        outOfStockProducts.push(prd.display_name);
                    }
                }
            });

            // Check if any products are out of stock
            if (restrict === true) {
                let warning = _t('Out of stock products cannot be sold: n') + outOfStockProducts.join(', n');
                this.env.services.pos.popup.add(ErrorPopup, {
                    title: _t('Out of Stock Products'),
                    body: _t(warning),
                });
            } else {
                // Proceed with payment if no products are out of stock
                super.pay();
            }
        } else {
            // Proceed with payment if zero quantity restriction is not enabled
            super.pay();
        }
    },
});

How can i make this for odoo 17?

I have tried searching about this but didn’t get any solution

2

Answers


  1. You should first specify it as follows in the module assets.

        'point_of_sale._assets_pos': [
            'your_module_name/static/src/js/your_js_filename.js'
        ],
    

    Then, your JavaScript code has to look like this: Odoo updated their asset directory on 16 and 17, so make sure your assets are listed as seen below.

    /** @odoo-module */
    
    import { Order } from "@point_of_sale/app/store/models";
    import { patch } from "@web/core/utils/patch";
    
    patch(Order.prototype, {
        async pay() {
            let order = this.env.services.pos.get_order();
            let lines = order.get_orderlines();
            let pos_config = this.env.services.pos.config;
            let config_id = this.env.services.pos.config.id;
            let prod_used_qty = {};
            let outOfStockProducts = [];
            let restrict = false;
            if (pos_config.restrict_zero_qty) {
                lines.forEach(line => {
                    let prd = line.product;
                    if (prd.type === 'product') {
                        if (prd.id in prod_used_qty) {
                            let old_qty = prod_used_qty[prd.id][1];
                            prod_used_qty[prd.id] = [prd.qty_available, line.quantity + old_qty];
                        } else {
                            prod_used_qty[prd.id] = [prd.qty_available, line.quantity];
                        }
    
                        // Check if product is out of stock
                        if (prd.qty_available <= 0) {
                            restrict = true;
                            outOfStockProducts.push(prd.display_name);
                        }
                    }
                });
    
                // Check if any products are out of stock
                if (restrict === true) {
                    let warning = _t('Out of stock products cannot be sold: n') + outOfStockProducts.join(', n');
                    this.env.services.pos.popup.add(ErrorPopup, {
                        title: _t('Out of Stock Products'),
                        body: _t(warning),
                    });
                } else {
                    // Proceed with payment if no products are out of stock
                    super.pay();
                }
            } else {
                // Proceed with payment if zero quantity restriction is not enabled
                super.pay();
            }
        },
    });
    
    Login or Signup to reply.
  2. You’ll need to tweak your event listener to trigger when a product is selected, not just during the payment process. In Odoo 17, you can override the default product selection behavior by modifying the click_product action in the POS JS. Look for the function responsible for adding products to the cart in your POS module, and insert your out-of-stock check there. If the product is out of stock, you can use showPopup to display your message instantly. Since you’re new to Odoo POS development, consider checking Odoo’s documentation or forums for examples on how to work with product selection events.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search