Ограничить событие нажатия кнопки внутри модального окна при открытии модального режима, а не кнопки с таким же классом вне его - PullRequest
0 голосов
/ 30 января 2019

Это сайт WooCommerce: https://www.mediatics -desa.com / desa / mako / shop /

Я добавил AJAX для добавления продуктов, и он отлично работает.Но если вы нажмете на изображение, откроется модальное окно с описанием продукта (не очень нужно, но клиенты хотят).Там есть еще одна кнопка, но когда я нажимаю на нее, она всегда вызывает функцию добавления в корзину дважды.Я предполагаю, что это потому, что он получает информацию дважды, потому что есть идентичные кнопки ... Но я не уверен, как решить это, не создавая беспорядок.Мои навыки JS немного ограничены.

Это функция (но весь код здесь: https://www.mediatics -desa.com / desa / mako / wp-content / themes / mako / js/main.js?ver=5.0.3):

function addCartAjax() {
    $('.single_add_to_cart_button').addClass('ajax_add_to_cart');

    $('.mako-producto').on('click', '.quantity .quantity-button', function () {
        return false;
    });

    $('.mako-producto').on('change input', '.quantity .qty', function () {
        var add_to_cart_button = $(this).parents(".mako-producto").find(".add_to_cart_button");
        // For AJAX add-to-cart actions
        add_to_cart_button.data('quantity', $(this).val());
        // For non-AJAX add-to-cart actions
        add_to_cart_button.attr('href', '?add-to-cart=' + add_to_cart_button.attr('data-product_id') + '&quantity=' + $(this).val());
    });

    $('.input-text.qty.text').bind('keyup mouseup', function () {
        var value = $(this).val();
        $('.product_quantity').val(value)
    });

    $('.mako-producto').on('click', '.ajax_add_to_cart', function(e) {
        console.log($(this));
        e.preventDefault();
        var $thisbutton = $(this);
        $('.ajaxerrors').remove();

        if ($thisbutton.is('.ajax_add_to_cart')) {
            $thisbutton.removeClass('added');
            $thisbutton.addClass('loading');
            var product_id = $(this).attr('data-product_id');
            var quantity = $(this).attr('data-quantity');
            var data = {
                action: 'bodycommerce_ajax_add_to_cart_woo_single',
                product_id: product_id,
                quantity: quantity
            }

            $('body').trigger('adding_to_cart', [$thisbutton, data]);
            $.post(wc_cart_fragments_params.ajax_url, data, function (response) {
                if (!response)
                    return;
                var this_page = window.location.toString();
                this_page = this_page.replace('add-to-cart', 'added-to-cart');
                if (response.error && response.product_url) {
                    window.location = response.product_url;
                    return;
                }
                if (wc_cart_fragments_params.cart_redirect_after_add === 'yes') {
                    window.location = wc_cart_fragments_params.cart_url;
                    return;
                } else {
                    $thisbutton.removeClass('loading');
                    var fragments = response.fragments;
                    var cart_hash = response.cart_hash;
                    if (fragments) {
                        $.each(fragments, function (key) {
                            $(key).addClass('updating');
                        });
                    }
                    $('.shop_table.cart, .updating, .cart_totals').fadeTo('400', '0.6').block({
                        message: null,
                        overlayCSS: {
                            opacity: 0.6
                        }
                    });
                    $thisbutton.addClass('added');
                    if (fragments) {
                        $.each(fragments, function (key, value) {
                            $(key).replaceWith(value);
                        });
                    }
                    $('.widget_shopping_cart, .updating').stop(true).css('opacity', '1').unblock();
                    $('.shop_table.cart').load(this_page + ' .shop_table.cart:eq(0) > *', function () {
                        $('.shop_table.cart').stop(true).css('opacity', '1').unblock();
                        $(document.body).trigger('cart_page_refreshed');
                    });
                    $('.cart_totals').load(this_page + ' .cart_totals:eq(0) > *', function () {
                        $('.cart_totals').stop(true).css('opacity', '1').unblock();
                    });
                }
            });
            return false;
        } else {
            return true;
        }
    });
}
$(function() {
    addCartAjax();
});

Большое спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...