Strategery-Inc / Magento2-InfiniteScroll

Automatically load the next page of products in Magento. Easy to install and configure, this module works 100% out of the box with vanilla Magento 2.x. For IAS license info: https://github.com/Strategery-Inc/Magento2-InfiniteScroll/issues/27#issuecomment-396148819
Academic Free License v3.0
57 stars 43 forks source link

Some help with jquery .on function #51

Closed gewaechshaus closed 5 years ago

gewaechshaus commented 5 years ago

Hey guys,

we are searching a good way to implement a jquery on function for items that are loaded by IAS. In our magento list.phtml we did include the following script, which is adding some the function to increase/decrease the quantity field on click.

 <script>
        require([
            'jquery'
        ], function($){

                var $ = jQuery.noConflict();

                $('.qtyplus').on( 'click', function(e) {
                    console.log('+ list');
                    e.preventDefault();
                    var currentVal = parseInt($(this).siblings('.qty').val());

                    if (!isNaN(currentVal)) {
                        $(this).siblings('.qty').val(currentVal + 1);
                    } else {
                        $(this).siblings('.qty').val(0);
                    }
                });

                $('.qtyminus').on( 'click', function(e) {
                    console.log('- list');
                    e.preventDefault();
                    var currentVal = parseInt($(this).siblings('.qty').val());

                    if (!isNaN(currentVal) && currentVal > 0) {
                        $(this).siblings('.qty').val(currentVal - 1);
                    } else {
                        $(this).siblings('.qty').val(0);
                    }
                });

        });
    </script> 

We tried to archive this by adding the functions to window.ias.on('rendered', function(items){ ... }.

The result is, that it is working for the loaded items, but also gets triggered for the items that are loaded by default. In conclusion, the quantity of the default items is increasing/decreasing by 2 instead of 1 on initial click.

What would be a good way to archive this, or more precise, how to reorder the structure?

Many thanks Jan

gsomoza commented 5 years ago

Hi @gewaechshaus – I was on holidays. Were you able to fix this?

gewaechshaus commented 5 years ago

Hi @gsomoza - thanks for your reply. Hope you had a nice holiday...

Yes, we were able to fix this. We created an overwrite in our custom theme and placed it in a function which is triggered in both cases.

gsomoza commented 5 years ago

Ah ok – glad to hear that!