2 products

Shopping cart

Your cart is empty.

Return to shop
close
// This script assumes jQuery is available in your Shopify theme. $(document).ready(function() { // Function to get product data (stock quantity) from Shopify function getProductStock(productId, callback) { $.get(`/products/${productId}.json`, function(data) { const product = data.product; const availableStock = product.variants[0].inventory_quantity; callback(availableStock); }); } // Handle "Add to Cart" button $('button#add-to-cart').click(function(e) { e.preventDefault(); const productId = $('input[name="id"]').val(); // Get the product variant ID const requestedQty = parseInt($('input[name="quantity"]').val(), 10); // Get the quantity user wants to buy // Get the available stock of the product getProductStock(productId, function(availableStock) { if (requestedQty > availableStock) { // Show error in custom popup with available stock showCustomPopup(`Sorry, we only have ${availableStock} items in stock.`); } else { // Proceed with adding to cart addToCart(productId, requestedQty); } }); }); // Handle "Buy Now" button $('button#buy-now').click(function(e) { e.preventDefault(); const productId = $('input[name="id"]').val(); // Get the product variant ID const requestedQty = parseInt($('input[name="quantity"]').val(), 10); // Get the quantity user wants to buy // Get the available stock of the product getProductStock(productId, function(availableStock) { if (requestedQty > availableStock) { // Show error in custom popup with available stock showCustomPopup(`Sorry, we only have ${availableStock} items in stock.`); } else { // Proceed with adding to cart and redirecting to checkout addToCartAndCheckout(productId, requestedQty); } }); }); // Function to add item to cart using Shopify's AJAX API function addToCart(productId, qty) { $.post('/cart/add.js', { items: [{ id: productId, quantity: qty }] }, function(response) { // Optionally show a success message alert('Item added to cart'); }).fail(function() { // Optionally handle any errors here alert('Error adding item to cart'); }); } // Function to add item to cart and immediately go to checkout function addToCartAndCheckout(productId, qty) { $.post('/cart/add.js', { items: [{ id: productId, quantity: qty }] }, function(response) { // Redirect to checkout window.location.href = '/checkout'; }).fail(function() { // Optionally handle any errors here alert('Error adding item to cart'); }); } // Function to show custom popup with a message function showCustomPopup(message) { // Create the popup if it doesn't exist already if ($('#custom-popup').length === 0) { $('body').append(` `); } // Set the message in the popup and show it $('#popup-message').text(message); $('#custom-popup').fadeIn(); // Close the popup when the "Close" button is clicked $('#close-popup').click(function() { $('#custom-popup').fadeOut(); }); } });