Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jQuery(document).ready(function($) {
- $('body').on('click', '.abcbiz_add_to_cart', function(e) {
- e.preventDefault();
- let button = $(this);
- if (button.is('.disabled')) {
- // Exit if the button is disabled
- return;
- }
- let product_id = button.data('product_id');
- button.addClass('abcbiz_cart_loading');
- let quantity = button.closest('form').find('.quantity input').val() || 1; // Default to 1 if not set
- $.ajax({
- type: 'POST',
- url: acbbiz_add_to_cart.ajax_url,
- data: {
- 'action': 'abcbiz_ajax_add_to_cart_handler',
- 'product_id': product_id,
- 'quantity': quantity,
- 'abcbiz_cart_nonce': acbbiz_add_to_cart.abcbiz_add_to_cart_nonce
- },
- success: function(response) {
- var messageDiv = $('#acbbiz-add-to-cart-message');
- if (response.success) {
- messageDiv.html('<p class="success-message">' + response.data.message + '</p>').fadeIn();
- } else {
- messageDiv.html('<p>' + response.data.message + '</p>').fadeIn();
- }
- messageDiv.delay(2000).queue(function(next) {
- $(this).empty();
- next();
- });
- },
- complete: function() {
- button.removeClass('abcbiz_cart_loading');
- },
- error: function(jqXHR, textStatus, errorThrown) {
- var messageDiv = $('#acbbiz-add-to-cart-message');
- var errorMessage = "An error occurred. Please try again.";
- if (jqXHR.responseJSON && jqXHR.responseJSON.data && jqXHR.responseJSON.data.message) {
- errorMessage = jqXHR.responseJSON.data.message;
- }
- messageDiv.html('<p class="error-message">' + errorMessage + '</p>').fadeIn().delay(2000).fadeOut();
- },
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement