🤩 File Manager - Mr.X
PHP:
8.2.33
Server:
Apache
OS:
Linux 5.14.0-611.49.1.el9_7.x86_64
User:
websparkit
Navigate
Upload:
Upload
New File
New Folder
Editing:cart.js
/* =================================== VELORA - Cart Functionality =================================== */ (function($) { 'use strict'; const Cart = { init: function() { this.events(); }, events: function() { // Add to cart $(document).on('click', '.add-to-cart-btn, .add-to-cart', function(e) { e.preventDefault(); const $btn = $(this); const productId = $btn.data('product-id'); const quantity = $btn.data('quantity') || $btn.closest('.product-form, form').find('input[name="quantity"]').val() || 1; const variation = $btn.data('variation') || ''; Cart.add(productId, quantity, variation, $btn); }); // Update cart quantity $(document).on('change', '.cart-qty-input', function() { const $input = $(this); const itemId = $input.data('item-id'); const quantity = parseInt($input.val()); if (quantity > 0) { Cart.update(itemId, quantity, $input); } }); // Remove from cart $(document).on('click', '.remove-cart-item', function(e) { e.preventDefault(); const $btn = $(this); const itemId = $btn.data('item-id'); Cart.remove(itemId, $btn.closest('tr, .cart-item')); }); // Apply coupon $(document).on('click', '#apply-coupon', function(e) { e.preventDefault(); const code = $('#coupon-code').val(); if (code) { Cart.applyCoupon(code); } }); }, add: function(productId, quantity, variation, $btn) { const originalHtml = $btn.html(); $btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Adding...'); $.ajax({ url: SITE_URL + 'ajax/cart.php', type: 'POST', data: { action: 'add', product_id: productId, quantity: quantity, variation: variation }, dataType: 'json', success: function(response) { if (response.success) { toastr.success(response.message); Cart.updateHeaderCount(response.cart_count); if (response.cart_total) { $('.cart-total').text(response.cart_total); } } else { toastr.error(response.message); } }, error: function() { toastr.error('Failed to add to cart. Please try again.'); }, complete: function() { if ($btn.length) { $btn.prop('disabled', false).html(originalHtml); } } }); }, update: function(itemId, quantity, $input) { $.ajax({ url: SITE_URL + 'ajax/cart.php', type: 'POST', data: { action: 'update', product_id: itemId, quantity: quantity }, dataType: 'json', success: function(response) { if (response.success) { toastr.success('Cart updated'); location.reload(); } else { toastr.error(response.message); } }, error: function() { toastr.error('Failed to update cart'); } }); }, remove: function(itemId, $item) { if (!confirm('Are you sure you want to remove this item?')) return; $item.css('opacity', '0.5'); $.ajax({ url: SITE_URL + 'ajax/cart.php', type: 'POST', data: { action: 'remove', product_id: itemId }, dataType: 'json', success: function(response) { if (response.success) { toastr.success('Item removed'); $item.fadeOut(300, function() { $(this).remove(); if ($('.cart-item, tbody tr').length === 0) { location.reload(); } else { location.reload(); } }); } else { toastr.error(response.message); $item.css('opacity', '1'); } }, error: function() { toastr.error('Failed to remove item'); $item.css('opacity', '1'); } }); }, applyCoupon: function(code) { $.ajax({ url: SITE_URL + 'ajax/cart.php', type: 'POST', data: { action: 'apply_coupon', code: code }, dataType: 'json', success: function(response) { if (response.success) { toastr.success(response.message); setTimeout(function() { location.reload(); }, 1000); } else { toastr.error(response.message); } }, error: function() { toastr.error('Failed to apply coupon'); } }); }, updateHeaderCount: function(count) { $('.header-action .badge-count').text(count); if (count > 0) { $('.header-action').first().find('.badge-count').show(); } } }; $(document).ready(function() { Cart.init(); }); })(jQuery);
Save Changes
Cancel
Create New File
Create New Folder