Reload ajax cart after update
I'm building a cart drawer for a shopfy site. The drawer is populated via the ajax cart api. I've also got some plus/minus functionality for the quantity of the products in the drawer.
This is the code for the cart drawer:
function updateCartDesc(data){
var _config = {
shopifyAjaxCartURL: '/cart.js'
};
$('#item_count').text(data.item_count);
renderHoverCart(data);
$('.drawer-overlay').css('display','block');
$.getJSON(_config.shopifyAjaxCartURL, function(cart) {
$.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;
$('.ajax-cart-form').append("<div class='cart-item desktop-full mobile-full'><div class='cart-image desktop-5'><a href='"+ cartItem.url +"'title='"+cartItem.title+"'><img src='"+ cartItem.image +"'></a></div><div class='cart-title desktop-7'><p>"+ cartItem.title +"</p><div class='cart-item-actions'><span class='drawer-price'><strong>£ "+ price +" GBP</strong></span></div><div class='cart-quantity'><input type='button' value='-' class='qtyminus qtyminus-2 CartCount qty-btn' field='updates_"+ cartItem.id +"'/><input name='updates' id='updates_"+ cartItem.id +"' class='quantity CartCount' value='"+ cartItem.quantity +"' /><input type='button' value='+' class='qtyplus qtyplus-2 CartCount qty-btn' field='updates_"+ cartItem.id +"' /></div></div>");
});
$(function(index, cartItem) {
var line2= index +1;
var cents2 = "";
if (cart.original_total_price % 100 < 10) {
cents2 = "0";
}
var price2 = parseInt(cart.original_total_price/100) + "." + cents2 + cart.original_total_price % 100;
$('.ajax-subtotal').append("<div class='drawer-subtotal'><h3>Subtotal <span class='money'>£"+ price2 +" GBP</span></h3></div>");
});
});
}
The cart quantity function looks like this:
$('.ajax-cart-form').on("click",'.qtyplus.qtyplus-2', function(){
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[id='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[id='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
$('input[id='+fieldName+']').val(0);
}
var currentVal2 = parseInt($('input[id='+fieldName+']').val());
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: { updates: { 35450129542: currentVal2 }},
dataType: 'json',
success: function() {
shopifyAjaxCartURL.load();
}
});
});
So this does update the quantity of the product, but the page needs to be refreshed first to display the update.
As you can see I've tried adding
success: function() {
shopifyAjaxCartURL.load();
}
I've also tried
success: function() {
shopifyAjaxCartURL.reload();
}
But the page still needs to be refreshed to see the updated cart. Any ideas how to do this?
javascript ajax
add a comment |
I'm building a cart drawer for a shopfy site. The drawer is populated via the ajax cart api. I've also got some plus/minus functionality for the quantity of the products in the drawer.
This is the code for the cart drawer:
function updateCartDesc(data){
var _config = {
shopifyAjaxCartURL: '/cart.js'
};
$('#item_count').text(data.item_count);
renderHoverCart(data);
$('.drawer-overlay').css('display','block');
$.getJSON(_config.shopifyAjaxCartURL, function(cart) {
$.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;
$('.ajax-cart-form').append("<div class='cart-item desktop-full mobile-full'><div class='cart-image desktop-5'><a href='"+ cartItem.url +"'title='"+cartItem.title+"'><img src='"+ cartItem.image +"'></a></div><div class='cart-title desktop-7'><p>"+ cartItem.title +"</p><div class='cart-item-actions'><span class='drawer-price'><strong>£ "+ price +" GBP</strong></span></div><div class='cart-quantity'><input type='button' value='-' class='qtyminus qtyminus-2 CartCount qty-btn' field='updates_"+ cartItem.id +"'/><input name='updates' id='updates_"+ cartItem.id +"' class='quantity CartCount' value='"+ cartItem.quantity +"' /><input type='button' value='+' class='qtyplus qtyplus-2 CartCount qty-btn' field='updates_"+ cartItem.id +"' /></div></div>");
});
$(function(index, cartItem) {
var line2= index +1;
var cents2 = "";
if (cart.original_total_price % 100 < 10) {
cents2 = "0";
}
var price2 = parseInt(cart.original_total_price/100) + "." + cents2 + cart.original_total_price % 100;
$('.ajax-subtotal').append("<div class='drawer-subtotal'><h3>Subtotal <span class='money'>£"+ price2 +" GBP</span></h3></div>");
});
});
}
The cart quantity function looks like this:
$('.ajax-cart-form').on("click",'.qtyplus.qtyplus-2', function(){
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[id='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[id='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
$('input[id='+fieldName+']').val(0);
}
var currentVal2 = parseInt($('input[id='+fieldName+']').val());
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: { updates: { 35450129542: currentVal2 }},
dataType: 'json',
success: function() {
shopifyAjaxCartURL.load();
}
});
});
So this does update the quantity of the product, but the page needs to be refreshed first to display the update.
As you can see I've tried adding
success: function() {
shopifyAjaxCartURL.load();
}
I've also tried
success: function() {
shopifyAjaxCartURL.reload();
}
But the page still needs to be refreshed to see the updated cart. Any ideas how to do this?
javascript ajax
add a comment |
I'm building a cart drawer for a shopfy site. The drawer is populated via the ajax cart api. I've also got some plus/minus functionality for the quantity of the products in the drawer.
This is the code for the cart drawer:
function updateCartDesc(data){
var _config = {
shopifyAjaxCartURL: '/cart.js'
};
$('#item_count').text(data.item_count);
renderHoverCart(data);
$('.drawer-overlay').css('display','block');
$.getJSON(_config.shopifyAjaxCartURL, function(cart) {
$.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;
$('.ajax-cart-form').append("<div class='cart-item desktop-full mobile-full'><div class='cart-image desktop-5'><a href='"+ cartItem.url +"'title='"+cartItem.title+"'><img src='"+ cartItem.image +"'></a></div><div class='cart-title desktop-7'><p>"+ cartItem.title +"</p><div class='cart-item-actions'><span class='drawer-price'><strong>£ "+ price +" GBP</strong></span></div><div class='cart-quantity'><input type='button' value='-' class='qtyminus qtyminus-2 CartCount qty-btn' field='updates_"+ cartItem.id +"'/><input name='updates' id='updates_"+ cartItem.id +"' class='quantity CartCount' value='"+ cartItem.quantity +"' /><input type='button' value='+' class='qtyplus qtyplus-2 CartCount qty-btn' field='updates_"+ cartItem.id +"' /></div></div>");
});
$(function(index, cartItem) {
var line2= index +1;
var cents2 = "";
if (cart.original_total_price % 100 < 10) {
cents2 = "0";
}
var price2 = parseInt(cart.original_total_price/100) + "." + cents2 + cart.original_total_price % 100;
$('.ajax-subtotal').append("<div class='drawer-subtotal'><h3>Subtotal <span class='money'>£"+ price2 +" GBP</span></h3></div>");
});
});
}
The cart quantity function looks like this:
$('.ajax-cart-form').on("click",'.qtyplus.qtyplus-2', function(){
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[id='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[id='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
$('input[id='+fieldName+']').val(0);
}
var currentVal2 = parseInt($('input[id='+fieldName+']').val());
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: { updates: { 35450129542: currentVal2 }},
dataType: 'json',
success: function() {
shopifyAjaxCartURL.load();
}
});
});
So this does update the quantity of the product, but the page needs to be refreshed first to display the update.
As you can see I've tried adding
success: function() {
shopifyAjaxCartURL.load();
}
I've also tried
success: function() {
shopifyAjaxCartURL.reload();
}
But the page still needs to be refreshed to see the updated cart. Any ideas how to do this?
javascript ajax
I'm building a cart drawer for a shopfy site. The drawer is populated via the ajax cart api. I've also got some plus/minus functionality for the quantity of the products in the drawer.
This is the code for the cart drawer:
function updateCartDesc(data){
var _config = {
shopifyAjaxCartURL: '/cart.js'
};
$('#item_count').text(data.item_count);
renderHoverCart(data);
$('.drawer-overlay').css('display','block');
$.getJSON(_config.shopifyAjaxCartURL, function(cart) {
$.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;
$('.ajax-cart-form').append("<div class='cart-item desktop-full mobile-full'><div class='cart-image desktop-5'><a href='"+ cartItem.url +"'title='"+cartItem.title+"'><img src='"+ cartItem.image +"'></a></div><div class='cart-title desktop-7'><p>"+ cartItem.title +"</p><div class='cart-item-actions'><span class='drawer-price'><strong>£ "+ price +" GBP</strong></span></div><div class='cart-quantity'><input type='button' value='-' class='qtyminus qtyminus-2 CartCount qty-btn' field='updates_"+ cartItem.id +"'/><input name='updates' id='updates_"+ cartItem.id +"' class='quantity CartCount' value='"+ cartItem.quantity +"' /><input type='button' value='+' class='qtyplus qtyplus-2 CartCount qty-btn' field='updates_"+ cartItem.id +"' /></div></div>");
});
$(function(index, cartItem) {
var line2= index +1;
var cents2 = "";
if (cart.original_total_price % 100 < 10) {
cents2 = "0";
}
var price2 = parseInt(cart.original_total_price/100) + "." + cents2 + cart.original_total_price % 100;
$('.ajax-subtotal').append("<div class='drawer-subtotal'><h3>Subtotal <span class='money'>£"+ price2 +" GBP</span></h3></div>");
});
});
}
The cart quantity function looks like this:
$('.ajax-cart-form').on("click",'.qtyplus.qtyplus-2', function(){
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[id='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[id='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
$('input[id='+fieldName+']').val(0);
}
var currentVal2 = parseInt($('input[id='+fieldName+']').val());
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: { updates: { 35450129542: currentVal2 }},
dataType: 'json',
success: function() {
shopifyAjaxCartURL.load();
}
});
});
So this does update the quantity of the product, but the page needs to be refreshed first to display the update.
As you can see I've tried adding
success: function() {
shopifyAjaxCartURL.load();
}
I've also tried
success: function() {
shopifyAjaxCartURL.reload();
}
But the page still needs to be refreshed to see the updated cart. Any ideas how to do this?
javascript ajax
javascript ajax
asked Nov 22 '18 at 11:22
MariaLMariaL
345223
345223
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429864%2freload-ajax-cart-after-update%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429864%2freload-ajax-cart-after-update%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown