Skip to content

Instantly share code, notes, and snippets.

@o-nkar
Created February 19, 2021 10:25
Show Gist options
  • Save o-nkar/8305e0ad77014e03335e95e5ae341d1e to your computer and use it in GitHub Desktop.
Save o-nkar/8305e0ad77014e03335e95e5ae341d1e to your computer and use it in GitHub Desktop.
function qTyJSCart(){
var d = document.createElement('div');
d.innerHTML = `<input type="text" name="quantity" value="1" size="2" id="Quantity" oninput="updatecartsticky(this.value, this.parentNode.dataset.id)" class="form-control">`;
document.querySelectorAll('#cart .info-item').forEach(ele=>{
var eleId = ele.querySelector('.cart-name').getAttribute('href').split('=');
var id = eleId[eleId.length - 1];
d.setAttribute('data-id', id);
ele.appendChild(d);
});
}
window.onload = qTyJSCart();
// function to handle on keyup over quantity
var updatecartsticky = function(qty, variant) {
if( qty && variant){
// data to be sent to the POST request
let _data = {
'id': variant,
'quantity': qty
}
fetch('/cart/change.js', {
method: "POST",
body: JSON.stringify(_data),
headers: {'Content-Type': 'application/json'},
})
.then(response => response.json())
.then(json => {
// fetching the cart and init
fetch('/cart.js')
.then(response => response.json())
.then( cartJson => loadCart(cartJson) );
})
.catch(err => console.log(err));
}
}
function loadCart(cart){
$('#cart .dropdown-menu > li:first-child .table > tbody > tr').remove();
$.each(cart.items, function (index, cartItem) {
var line = index + 1;
var cents = "";
if (cartItem.price % 100 < 10) {
cents = "0";
}
var price = parseInt(cartItem.price / 100) + "." + cents + cartItem.price % 100;
var cartRemove = 'onclick="cart.remove(\''+cartItem.id+'\')"';
var cartItem = "<tr><td class='text-center'><a href='" + cartItem.url + "' title='" + cartItem.title + "'><img class='cart-image' src='" + cartItem.image + "' alt='" + cartItem.title + "' title='" + cartItem.title + "'></a></td><td class='text-left info-item'><a class='cart-name' href='" + cartItem.url + "'>" + cartItem.title + "</a><p class='cart-quantity'>" + cartItem.quantity + " &times;</p><p class='cart-price'><span class='money'>" + Shopify.formatMoney(price, moneyFormat) + "</span></p></td><td class='text-center cart-close'><button type='button' "+cartRemove+"' title='Remove' class='btn btn-danger btn-xs'><i class='ion-android-close'></i></button></td></tr>";
$('#cart .dropdown-menu > li:first-child .table > tbody').append(cartItem);
});
if (cart.item_count < 1) {
$('#cart .dropdown-menu > li:first-child .table > tbody').append('<p class="text-center cart-empty">Sepetin boş!</p>');
$('#cart .dropdown-menu li:not(:first-child):last-child').removeClass('show');
$('#cart .dropdown-menu li:not(:first-child):last-child').addClass('hide');
} else {
$('#cart .dropdown-menu p.cart-empty').remove();
$('#cart .dropdown-menu li:not(:first-child):last-child').removeClass('hide');
$('#cart .dropdown-menu li:not(:first-child):last-child').addClass('show');
}
$('#cart > button #cart-total .txt_number').html(cart.item_count);
$('#cart > button .total-price').html('<span class=money>'+Shopify.formatMoney(cart.total_price, moneyFormat)+'<i class="fas fa-chevron-down"></i></span>');
$('#cart-subtotal').html('<span class=money>'+Shopify.formatMoney(cart.total_price, moneyFormat)+'</span>');
// reinit the JS code
qTyJSCart();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment