WordPress 6.3 to 6.4 now getting JS error in my plugin
-
I wrote what is a relatively simple plugin that allows users on my woocommerce store to add a free sample to their basket, it’s been working great until wordpress auto updated to 6.5, now when the users clicks to add a sample to the basket they get this error,
Cannot read properties of undefined (reading ‘0’)
with this stacktrace,
Uncaught TypeError: Cannot read properties of undefined (reading '0') at document.body.onadded_to_cart (main.js?ver=c4a77335eae7e6132482:1:3147) at Object.trigger (jquery.min.js?ver=3.7.1:2:70175) at HTMLBodyElement.<anonymous> (jquery.min.js?ver=3.7.1:2:70726) at Function.each (jquery.min.js?ver=3.7.1:2:3129) at e.<computed>.each (jquery.min.js?ver=3.7.1:2:1594) at e.<computed>.trigger (jquery.min.js?ver=3.7.1:2:70701) at HTMLButtonElement.<anonymous> (gifts-at-checkout.js?ver=6.5:112:26) at HTMLLIElement.dispatch (jquery.min.js?ver=3.7.1:2:40035) at v.handle (jquery.min.js?ver=3.7.1:2:38006)The
gifts-at-checkout.jsis the javascript my plugin loads in and the line is this,$(document.body).trigger('added_to_cart', [$thisbutton, data]);which is one line of this click event.
$( 'li' ).on( 'click', '.single_add_to_cart_button', function(e) { e.preventDefault(); var $thisbutton = $(this), $form = $thisbutton.closest('form.cart'), id = $thisbutton.val(), product_qty = $form.find('input[name=quantity]').val() || 1, product_id = id, variation_id = $form.find('input[name=variation_id]').val() || 0; var data = { action: 'woocommerce_ajax_add_to_cart', product_id: product_id, product_sku: '', quantity: product_qty, variation_id: variation_id, }; $(document.body).trigger('added_to_cart', [$thisbutton, data]); $.ajax({ type: 'post', url: wc_add_to_cart_params.ajax_url, data: data, beforeSend: function (response) { $thisbutton.removeClass('added').addClass('loading'); }, complete: function (response) { $thisbutton.addClass('added').removeClass('loading'); $thisbutton.parent().addClass("in-cart"); }, success: function (response) { if (response.error && response.product_url) { window.location = response.product_url; return; } else { $(document.body).trigger('update_checkout', [response.fragments, response.cart_hash, $thisbutton]); selectedSamples.push(product_id); if(selectedSamples.length == parseInt(scriptParams.samples_threshold)) { $('li .single_add_to_cart_button').addClass('isDisabled'); } } }, }); return false; });I assume that something in wordpress core in woocommerce core has changed but I cannot for the life of me figure out what, can anyone shed any light on this for me?
Many thanks
The topic ‘WordPress 6.3 to 6.4 now getting JS error in my plugin’ is closed to new replies.