Jquery each does not work

0

Hi, I'm creating a shoping cart and I'm using DCAJAXPaypalCart but the data is not added for every spoc class I find.

DCAJAXPaypalCart Here's where the shoping cart example is

 $(function () {
    var cart = $('#cart').DCAJAXPaypalCart({
        width: 800,
        openNewCheckOutWindow: true,
        //themeColor:'#333',
        //themeDarkColor:'#FFF',
        header: 'AJAX Cart Demo',
        footer: 'We accpet paypal, visa and master card. (This is a customizable footer)',
        paypalOptions: {
            business: '[email protected]',

        }

    });

    $('.spoc').each(function (index, element) {
        alert($(this).attr('data-id') + "  " + $(this).attr('data-name') + " " + $(this).attr('data-price') + " " + $(this).attr('data-image'));
        cart.addBuyButton('#' + $(this).attr('data-id'), {
            id: $(this).attr('data-id'),
            name: $(this).attr('data-name'),                     // Item name appear on the cart
            thumbnail: 'imgs/llantas_menu/' + $(this).attr('data-image'),      // Thumbnail path of the item (Optional)
            price: $(this).attr('data-price')                     // Cost of the item
            // Shipping cost for the item (Optional)

        });
        return cart;
    });

});
    
asked by Godeolo 01.08.2017 в 23:13
source

1 answer

0

There is a conceptual error in your logic. Why are you running the "addBuyButton" function (that is, "add purchase button") in the click event of another button? That is terrible: you are adding a button every time you press the button.

No: addBuyButton adds them to the load event of the page, and then the user uses those buttons to buy things.

I do not know this plugin, but I suspect you can do something like this:

$(function () {
    var cart = $('#cart').DCAJAXPaypalCart({
        width: 800,
        openNewCheckOutWindow: true,
        //themeColor:'#333',
        //themeDarkColor:'#FFF',
        header: 'AJAX Cart Demo',
        footer: 'We accpet paypal, visa and master card. (This is a customizable footer)',
        paypalOptions: {
            business: '[email protected]',

        }

    });
    
    $('.button').each(function() {
        cart.addBuyButton('#' + $(this).attr('data-id'), {
            id: $(this).attr('data-id'),
            name: $(this).attr('data-name'),                     // Item name appear on the cart
            thumbnail: 'imgs/llantas_menu/' + $(this).attr('data-image'),      // Thumbnail path of the item (Optional)
            price: $(this).attr('data-price')                     // Cost of the item
            // Shipping cost for the item (Optional)

        });

    });

});
    
answered by 02.08.2017 в 08:39