Is there a way to add custom data to the cart using javascript? The data is from URL query params. I have tried using woocommerce_add_cart_item_data
and setting query-vars etc, but my data is not populated at the time I click the add to cart button. I am thinking maybe the $( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
event but I am not sure how to go about adding the url params as extra data. Here is my code so far.
$(document.body).on('adding_to_cart', function( button, data ) {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
//the url params that i want to add as custom cart data
var color = urlParams.get('color'),
hexcode = urlParams.get('hex'),
extracost = urlParams.get('price')
})