Kriuchko
1/8/2019 - 3:47 PM

Ajax

Magento 2 ajax

https://www.mageplaza.com/review/ajax-cart/

\!h ajax query on product page FORM_KEY needed

<script>
    require([
        'jquery',
        'prototype',
    ], function(jQuery){
        var BUNDLE_SELECTOR = '<?php echo $block->getBundleSelector() ?>';
        var BUNDLE_PRICE_SELECTOR = '<?php echo $block->getBundlePriceSelector() ?>';
        const ORIGINAL_INSTALMENT_PRICE = jQuery('.prospapay-instalment-amount').text();

        jQuery(BUNDLE_SELECTOR).on('click', function() {
            setTimeout(function() {
                CURRENT_PRICE = jQuery(BUNDLE_PRICE_SELECTOR).text().replace(/[^0-9.-]+/g,"");
                FORM_KEY = '<?php echo $block->getFormKey(); ?>';

                if (CURRENT_PRICE > 0) {
                    new Ajax.Request('<?php echo $block->getUrl('prospapay/index/calculate') ?>', {
                        method: 'get',
                        parameters: {price: CURRENT_PRICE},
                        dataType: "html",
                        onSuccess: function(transport) {
                            if (transport.status == 200){
                                jQuery('.prospapay-instalment-amount').text(transport.responseText);
                            }
                        }
                    });
                } else {
                    jQuery('.prospapay-instalment-amount').text(ORIGINAL_INSTALMENT_PRICE);
                }
            }, 1);
        });
    });
</script>