Changing the quantity of multiple products in a product bundle at once

Viewed 15

I am making a custom javascript functionality in my webshop, where I can define multiple predefined product quantities for a product bundle which gives predefined options for different ages. One product bundle (a knitting recipe) will have several products where the quantity of each product follows a predefined table based on the desired size of the product.

I've set up a product bundle successfully, and the functionality to define these predefined values - however, when I apply this on the webpage, I cannot get the price to calculate correctly if the buttons adjust the quantities for more than one of the bundled products.

Here is the current code

    for (const product of data) {
        let $input = document.querySelector(`.bundled_item_cart_content[data-product_id="${product.product_id}"] input.qty`);
        if (!$input) {
            $input = document.querySelector(`.bundled_item_cart_content[data-product_id="${product.product_id}"]`).closest('tr').querySelector(`input.qty`);
        }

        $input.value = product.quantity;
        a = new Event("change", {bubbles: !0})
        $input.dispatchEvent(a);                        
    }

I have tried several ways of updating the values and firing the change event (including jquery), and regardless of how I manage to trigger the change() event on the quantity inputs, the end result is always that when the total price calculates, it only applies the new values for the final item that was changed, the others get ignored. The values seems to be updated correctly, because if I open the browser console after the code is being run, and call the change() event directly on either of the quantity inputs without any quantity changes (like this: $('bundle_item_3 input.qty').change()), the total price is updated correctly.

However I cannot get the price to calculate correctly in the initial click event, it always skips everything but the last item in the price calculation. If I change the order of the product ids, the same behavior happens, but with a different item being correct.

This seems to be identical to this issue which has no answers: Adjusting Bundle Product Price Programmatically

I'm happy to provide access to the code if anyone can help me debug this.

0 Answers
Related