All things Shopify and commerce
How to open drawer cart, after add to cart action?
I'm using the Fastor theme, Which was deleted from themeforest 1 day after i purchased the theme.
So i need assistance from you guys...
website link
marsedo.com.tr
Solved! Go to the solution
This is an accepted solution.
Hi @marsedo
Here is the revised code with Total, Cart & Checkout buttons issue fixed
<script>
document.addEventListener("DOMContentLoaded", function() {
$('.product-form__cart-submit').on('click', function(e) {
e.preventDefault();
const $form = $(this).closest('form');
$.ajax({
type: 'POST',
url: '/cart/add.js',
data: $form.serialize(),
dataType: 'json',
success: function(item) {
$('.cart-dropdown').addClass('opened');
// Wait and then update cart drawer
setTimeout(updateCartDrawer, 300);
},
error: function(xhr) {
alert(xhr.responseJSON?.description || 'Ürün sepete eklenemedi.');
}
});
});
function updateCartDrawer() {
$.getJSON('/cart.js', function(cart) {
const $drawerBox = $('.dropdown-box'); // your main wrapper
let $productWrapper = $drawerBox.find('.products');
// If cart is empty
if (cart.items.length === 0) {
$productWrapper.replaceWith('<div class="products no-cart-items text-center mt-4">Sepetiniz şu anda boş.</div>');
// Remove total and actions if they exist
$drawerBox.find('.cart-total').remove();
$drawerBox.find('.cart-action').remove();
// Update cart count
$('.cart-count').text('0');
return;
}
// If .products.scrollable doesn't exist yet, create it
if (!$drawerBox.find('.products.scrollable').length) {
$productWrapper.replaceWith('<div class="products scrollable"></div>');
}
const $drawer = $drawerBox.find('.products.scrollable');
let fullHTML = '';
cart.items.forEach(function(item, index) {
const imageUrl = item.image ? (item.image.startsWith('http') ? item.image : 'https:' + item.image) : '';
fullHTML += `
<div class="product product-cart"
data-cart-item
data-cart-item-key="${item.key}"
data-cart-item-url="${item.url}"
data-cart-item-title="${item.product_title} - ${item.variant_title}"
data-cart-item-index="${index + 1}"
data-cart-item-quantity="${item.quantity}">
<figure class="product-media">
<a href="${item.url}">
<img class="cart__image" src="${imageUrl}" alt="${item.product_title}">
</a>
<button class="btn btn-link btn-close cart__remove" data-line="${index + 1}">
<i class="la la-times"></i>
</button>
</figure>
<div class="product-detail">
<a href="${item.url}" class="product-name">${item.product_title} - ${item.variant_title}</a>
<div class="price-box">
<span class="product-quantity">${item.quantity}</span>
<span class="product-price">${(item.final_line_price / 100).toLocaleString('tr-TR')}TL</span>
</div>
</div>
</div>
`;
});
$drawer.html(fullHTML);
// Add cart-total if not exists
if (!$drawerBox.find('.cart-total').length) {
$drawer.after(`
<div class="cart-total">
<label>Alt toplam</label>
<span class="price">${(cart.total_price / 100).toLocaleString('tr-TR')}TL</span>
</div>
`);
} else {
$drawerBox.find('.cart-total .price').text((cart.total_price / 100).toLocaleString('tr-TR') + 'TL');
}
// Add cart-action if not exists
if (!$drawerBox.find('.cart-action').length) {
$drawerBox.append(`
<div class="cart-action">
<a href="/cart" class="btn btn-dark btn-link">Sepeti Görüntüle</a>
<a href="/checkout" class="btn btn-dark">Ödeme</a>
</div>
`);
}
// Update cart count
$('.cart-count').text(cart.item_count);
});
}
// Event listener for cart item removal
$(document).on('click', '.cart__remove', function(e) {
e.preventDefault();
const line = $(this).data('line'); // Gets the line index
$.ajax({
type: 'POST',
url: '/cart/change.js',
data: {
line: line,
quantity: 0
},
dataType: 'json',
success: function(updatedCart) {
updateCartDrawer(); // Refresh the drawer content
},
error: function(xhr) {
console.error('Failed to remove item:', xhr);
}
});
});
});
</script>
Hi @marsedo
Can you please go to Customize and then select Settings and find cart-related settings you will find a redirect to the cart or open drawer settings there, Not exactly sure as the theme is not free, and not able to find it.
Check the screenshots below they are from the DAWN Theme from Shopify. You need to find the similar settings from your theme.
We only have Page & Popup Notification options from theme settings
Hi @marsedo
I have found the solution to this,
I have created a custom script for this.
Got to the Current live theme -> Edit code -> Find theme.liquid and add the below code before </body>
<script>
document.addEventListener("DOMContentLoaded", function() {
$('.product-form__cart-submit').on('click', function(e) {
e.preventDefault();
const $form = $(this).closest('form');
$.ajax({
type: 'POST',
url: '/cart/add.js',
data: $form.serialize(),
dataType: 'json',
success: function(item) {
$('.cart-dropdown').addClass('opened');
// Wait and then update cart drawer
setTimeout(updateCartDrawer, 300);
},
error: function(xhr) {
alert(xhr.responseJSON?.description || 'Ürün sepete eklenemedi.');
}
});
});
function updateCartDrawer() {
$.getJSON('/cart.js', function(cart) {
// Handle case where .products.scrollable doesn't exist yet
if (!$('.products.scrollable').length) {
// Replace the empty cart div with a new container
$('.products.no-cart-items').replaceWith('<div class="products scrollable"></div>');
}
const $drawer = $('.products.scrollable');
let fullHTML = '';
if (cart.items.length === 0) {
$drawer.html('<div class="text-center mt-4">Sepetiniz şu anda boş.</div>');
return;
}
cart.items.forEach(function(item, index) {
const imageUrl = item.image ? (item.image.startsWith('http') ? item.image : 'https:' + item.image) : '';
fullHTML += `
<div class="product product-cart"
data-cart-item
data-cart-item-key="${item.key}"
data-cart-item-url="${item.url}"
data-cart-item-title="${item.product_title} - ${item.variant_title}"
data-cart-item-index="${index + 1}"
data-cart-item-quantity="${item.quantity}">
<figure class="product-media">
<a href="${item.url}">
<img class="cart__image" src="${imageUrl}" alt="${item.product_title}">
</a>
<button class="btn btn-link btn-close cart__remove" data-line="${index + 1}">
<i class="la la-times"></i>
</button>
</figure>
<div class="product-detail">
<a href="${item.url}" class="product-name">${item.product_title} - ${item.variant_title}</a>
<div class="price-box">
<span class="product-quantity">${item.quantity}</span>
<span class="product-price">${(item.final_line_price / 100).toLocaleString('tr-TR')}TL</span>
</div>
</div>
</div>
`;
});
$drawer.html(fullHTML);
$('.cart-count').text(cart.item_count); // Update badge if needed
});
}
// Event listener for cart item removal
$(document).on('click', '.cart__remove', function(e) {
e.preventDefault();
const line = $(this).data('line'); // Gets the line index
$.ajax({
type: 'POST',
url: '/cart/change.js',
data: {
line: line,
quantity: 0
},
dataType: 'json',
success: function(updatedCart) {
updateCartDrawer(); // Refresh the drawer content
},
error: function(xhr) {
console.error('Failed to remove item:', xhr);
}
});
});
});
</script>
This solution worked perfectly.
Thank you for the support!
@Huptech-Web so after i apply it live, i discovered that, "go to checkout" & "view cart" buttons dont appear on the drawer cart. So customers cant buy now... Could you revise the code?
This is an accepted solution.
Hi @marsedo
Here is the revised code with Total, Cart & Checkout buttons issue fixed
<script>
document.addEventListener("DOMContentLoaded", function() {
$('.product-form__cart-submit').on('click', function(e) {
e.preventDefault();
const $form = $(this).closest('form');
$.ajax({
type: 'POST',
url: '/cart/add.js',
data: $form.serialize(),
dataType: 'json',
success: function(item) {
$('.cart-dropdown').addClass('opened');
// Wait and then update cart drawer
setTimeout(updateCartDrawer, 300);
},
error: function(xhr) {
alert(xhr.responseJSON?.description || 'Ürün sepete eklenemedi.');
}
});
});
function updateCartDrawer() {
$.getJSON('/cart.js', function(cart) {
const $drawerBox = $('.dropdown-box'); // your main wrapper
let $productWrapper = $drawerBox.find('.products');
// If cart is empty
if (cart.items.length === 0) {
$productWrapper.replaceWith('<div class="products no-cart-items text-center mt-4">Sepetiniz şu anda boş.</div>');
// Remove total and actions if they exist
$drawerBox.find('.cart-total').remove();
$drawerBox.find('.cart-action').remove();
// Update cart count
$('.cart-count').text('0');
return;
}
// If .products.scrollable doesn't exist yet, create it
if (!$drawerBox.find('.products.scrollable').length) {
$productWrapper.replaceWith('<div class="products scrollable"></div>');
}
const $drawer = $drawerBox.find('.products.scrollable');
let fullHTML = '';
cart.items.forEach(function(item, index) {
const imageUrl = item.image ? (item.image.startsWith('http') ? item.image : 'https:' + item.image) : '';
fullHTML += `
<div class="product product-cart"
data-cart-item
data-cart-item-key="${item.key}"
data-cart-item-url="${item.url}"
data-cart-item-title="${item.product_title} - ${item.variant_title}"
data-cart-item-index="${index + 1}"
data-cart-item-quantity="${item.quantity}">
<figure class="product-media">
<a href="${item.url}">
<img class="cart__image" src="${imageUrl}" alt="${item.product_title}">
</a>
<button class="btn btn-link btn-close cart__remove" data-line="${index + 1}">
<i class="la la-times"></i>
</button>
</figure>
<div class="product-detail">
<a href="${item.url}" class="product-name">${item.product_title} - ${item.variant_title}</a>
<div class="price-box">
<span class="product-quantity">${item.quantity}</span>
<span class="product-price">${(item.final_line_price / 100).toLocaleString('tr-TR')}TL</span>
</div>
</div>
</div>
`;
});
$drawer.html(fullHTML);
// Add cart-total if not exists
if (!$drawerBox.find('.cart-total').length) {
$drawer.after(`
<div class="cart-total">
<label>Alt toplam</label>
<span class="price">${(cart.total_price / 100).toLocaleString('tr-TR')}TL</span>
</div>
`);
} else {
$drawerBox.find('.cart-total .price').text((cart.total_price / 100).toLocaleString('tr-TR') + 'TL');
}
// Add cart-action if not exists
if (!$drawerBox.find('.cart-action').length) {
$drawerBox.append(`
<div class="cart-action">
<a href="/cart" class="btn btn-dark btn-link">Sepeti Görüntüle</a>
<a href="/checkout" class="btn btn-dark">Ödeme</a>
</div>
`);
}
// Update cart count
$('.cart-count').text(cart.item_count);
});
}
// Event listener for cart item removal
$(document).on('click', '.cart__remove', function(e) {
e.preventDefault();
const line = $(this).data('line'); // Gets the line index
$.ajax({
type: 'POST',
url: '/cart/change.js',
data: {
line: line,
quantity: 0
},
dataType: 'json',
success: function(updatedCart) {
updateCartDrawer(); // Refresh the drawer content
},
error: function(xhr) {
console.error('Failed to remove item:', xhr);
}
});
});
});
</script>
Currently, your product form is being submitted to the server in a direct "native" way.
Visitor is immediately redirected to the cart page.
What you're looking for is adding to cart with JS, also known as Ajax cart.
Your theme has a switch to select one way or another -- you need to check theme settings.
When enabled, adding to cart will show a popup like this:
Unfortunately, can't be more specific then this -- have not seen this theme "from the inside" in a while.
We only have Page & Popup Notification options from theme settings.
And the show popup notification option does not work!
Hmm. It works, but it's temperamental (or how I've got the screenshot above if it does not?).
For example, it works for https://marsedo.com.tr/products/tookes-kadin-gunluk-sivri-burun-gold-aksesuar-detayli-babet?variant=... but does not for some others.
Anyways, it's possible to do what you want, but will require theme code change, which is not trivial. Would need some digging and testing.
Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025