javascriptodooodoo-16

in odoo How to add product in POS programmatically


I want to add a product line in POS but I face some errors

this my code

                const productId = 293;
                const sel_product = await this.rpc({
                    model: "product.product",
                    method: "read",
                    args: [[productId]],
                });

                console.log("sel_product :", sel_product[0]);
                const newItem = {
                    id: productId,
                    product: sel_product[0],
                    qty: 1,
                    price: -300,
                };

                var Orderline = models.Orderline;
                var line = new Orderline({}, {
                      pos: this.env.pos,
                      order: this.env.pos.get_order(),
                      product: newItem.product});

                line.set_quantity(newItem.qty);
                this.env.pos.get_order().add_product(line);

and I face some error

error fetching promotions: TypeError: this.product.get_unit is not a function
    at Orderline.get_unit 
    at Orderline.set_quantity

can any one help me on this


Solution

  • I found a solution

    with this code

    var product = this.env.pos.db.get_product_by_id(productId);
    var order = this.env.pos.get_order();
    if (product) {
        order.add_product(product);
    } else {
        console.error("Error Not there");
    }