Skip to content

Instantly share code, notes, and snippets.

@tobymarsden
Last active July 9, 2019 22:34
Show Gist options
  • Save tobymarsden/c2dde984a41fcbd11ceeed711c324056 to your computer and use it in GitHub Desktop.
Save tobymarsden/c2dde984a41fcbd11ceeed711c324056 to your computer and use it in GitHub Desktop.
Ella theme line item properties
initProductAddToCart: function() {
if ($('#product-add-to-cart').length > 0) {
$('#product-add-to-cart').click(function(event) {
event.preventDefault();
if ($(this).attr('disabled') != 'disabled') {
if (!window.ajax_cart) {
$(this).closest('form').submit();
} else {
var variant_id = $('#add-to-cart-form select[name=id]').val();
if (!variant_id) {
variant_id = $('#add-to-cart-form input[name=id]').val();
}
var quantity = $('#add-to-cart-form input[name=quantity]').val();
if (!quantity) {
quantity = 1;
}
var title = $('.product-title h2').html();
var image = $('#product-featured-image').attr('src');
var $properties = $(this).closest('form').find('[name^="properties"]');
var properties = {};
$($properties).each(function(index, el){
var name = $(el).attr('name').substr(11, $(el).attr('name').length - 12)
properties[name] = $(el).val();
});
ella.doAjaxAddToCart(variant_id, quantity, title, image, properties);
}
}
return false;
});
}
},
// ...
doAjaxAddToCart: function(variant_id, quantity, title, image, properties) {
var data = {}
data.quantity = quantity
data.id = variant_id
data.properties = properties
$.ajax({
type: "post",
url: "/cart/add.js",
data: data, //'quantity=' + quantity + '&id=' + variant_id,
dataType: 'json',
beforeSend: function() {
ella.showLoading();
},
success: function(msg) {
ella.hideLoading();
$('.ajax-success-modal').find('.ajax-product-title').html(ella.translateText(title));
$('.ajax-success-modal').find('.ajax-product-image').attr('src', image);
$('.ajax-success-modal').find('.btn-go-to-wishlist').hide();
$('.ajax-success-modal').find('.btn-go-to-cart').show();
ella.showModal('.ajax-success-modal');
ella.updateDropdownCart();
},
error: function(xhr, text) {
ella.hideLoading();
$('.ajax-error-message').text($.parseJSON(xhr.responseText).description);
ella.showModal('.ajax-error-modal');
}
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment