Skip to content

Instantly share code, notes, and snippets.

@ventouris
Created July 27, 2017 09:55
Show Gist options
  • Save ventouris/d300dc8726e1d31d32f33576d2aa8290 to your computer and use it in GitHub Desktop.
Save ventouris/d300dc8726e1d31d32f33576d2aa8290 to your computer and use it in GitHub Desktop.
// The button to add the regular product
<a class="btn btn-addcart add_to_cart" data-count="1" data-pid="284">Add to cart</a>
// dropbox created dynamically from backend
<div class="add-on-service talktime-options hidden-box" id="talktime-options-284" style="display: block;">
<select class="form-control" id="select-extra-time-284">
<option>Please select an option</option>
<option data-attributename="pa_additional-talktime" data-slug="30mins" value="7804">30mins (+$3.00)</option>
<option data-attributename="pa_additional-talktime" data-slug="60mins" value="7805">60mins (+$6.00)</option>
</select>
</div>
// jQuery code
function addToCart(product_id, variable_id, variable_attribute_name, variable_slug, amount, ) {
jQuery.ajax({
type: 'POST',
url: '?add-to-cart='+ product_id + '&variation_id=' + variable_id + '&attribute_' + variable_attribute_name + '=' + variable_slug + '&quantity=' + amount,
data: { 'product_id': product_id, 'quantity': amount},
success: function(response, textStatus, jqXHR){
console.log("Product added");
}
});
}
jQuery('.add_to_cart').click(function(e) {
e.preventDefault();
product_id = jQuery(e.currentTarget).data('pid');
variable_id = jQuery('#select-extra-time-' + product_id).val();
variable_attribute_name = jQuery('#select-extra-time-' + product_id + ' option:selected').data('attributename');
variable_slug = jQuery('#select-extra-time-' + product_id + ' option:selected').data('slug');
amount = jQuery(e.currentTarget).data('count');
addToCart(product_id, variable_id, variable_attribute_name, variable_slug, amount);
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment