Avoiding the ability to repeatedly click the "add to cart" button in WordPress/WooCommerce

I am looking for a solution to prevent double clicking on the add to cart button in WordPress. Currently, I can click on the button multiple times and it causes issues with the product quantity in the cart page. The cart page incorrectly shows that the product already exists in the cart due to the double clicks.

Even after trying the code below, the issue persists:

do_action('woocommerce_before_shop_loop_item');

function onlyonce(){ ?>
<script>
jQuery(document).ready(function($){
$('.cart').one('submit', function() {
$(this).find('button[type="submit"]').attr('disabled','disabled');
});
});

</script>
<?php }

https://i.stack.imgur.com/LPwaT.jpg

Answer №1

If you want to improve user experience, consider disabling the button on a click event instead of upon submission.

Feel free to test out the code snippet below and see if it works for your needs.

Note: This code serves as a reference only; I will remove it once you confirm its effectiveness.

jQuery(document).ready(function($) {

  $('#submitBtn').on('click', function() {
    console.log('disabling');
    $(this).attr('disabled', 'disabled');
    console.log('disabled');

    // then submit your form
  });
  /*
    $('.cart').one('submit', function() {
      $(this).find('button[type="submit"]').attr('disabled', 'disabled');
    });
    */
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="submitBtn" type="submit">Checkout</button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent header from being displayed in XML response in Symfony 2

I am currently working on generating an xml document using Symfony. The generation process is successful and I am returning the document using a Twig template named sitemap.xml.twig as shown below: <?xml version="1.0" encoding="UTF-8"?> <urlset x ...

Ways to navigate through a webpage without encountering any overflow issues

My window is too small to scroll, but I still need the ability to do so. Is it possible to scroll even when the height of the container is not large enough to display the scrollbar? Below is the code I am using to achieve scrolling: setTimeout(function() ...

Quickly view products in Opencart will automatically close after adding them to the cart and redirecting the

I've integrated the product quickview feature into my OpenCart theme, which opens a product in a popup. However, when I add a product to the cart from the popup, it doesn't update on the main page until I refresh. I'm looking for a way to re ...

Handling MethodNotAllowedHttpException in IIS, AngularJS, and Laravel: A Comprehensive Guide

Currently, I am diving into the world of AngularJS 1.5.8 + Laravel 5.3.17 + PHP 7 hosted on IIS/Windows 10 by following this helpful tutorial. However, when attempting to send a HTTP request using AngularJS $resource.delete() to Laravel, I keep encounteri ...

PHP: Retrieving the current time zone from the database

In my database, I save dates in DATETIME format as Y-m-d H:i:s. My goal is to display the date from the database in the user's local time. For instance, if the user is located in France, I want to show their timezone. Is there a way to accomplish thi ...

Saving data from AJAX queries

How can I access and store variables from an ajax request that is returned from a function? While I am able to console.log the variable, I'm having difficulty saving it outside of the scope. This is how it appears: In the HTML: function my_ajax() ...

Joomla update query malfunctioning - seeking resolution

Currently, I am in the process of developing a Joomla extension and I am attempting to update entries within my Joomla extensions database table using the code snippet below in my model: $this->_db->setQuery( $this->_db->getQu ...

Wordpress AJAX request results in a value not found

** I have added more details, thank you for your assistance so far I am in the process of implementing an AJAX search functionality on my Wordpress site to filter properties based on type, location, and status. Being a novice at AJAX, I have been followi ...

Turn off the CSS hover effect for a custom button if the ng disabled attribute is set to true

My goal is to disable custom buttons, and the disable(pointer) function is working correctly. The issue arises when we try to hover on the button, as the hover effect still works. I therefore need to disable hover as well. I am looking to apply ng-disabl ...

Modifying the input field's name attribute while utilizing multiple datasets in typeahead.js

I am currently utilizing typeahead.js with multiple datasets, following the guidance provided here. I have chosen not to employ Bloodhound in my implementation, resulting in some differences in my code structure, outlined below: $('#search .typeahead ...

Should a checkbox be added prior to the hyperlink?

html tags <ul class="navmore"> <li><a href="link-1">Link 1</a></li> <li><a href="link-2">Link 2</a></li> </ul> Jquery Implementation in the footer $(".navmore li a").each(function(){ v ...

Retrieve the Corresponding Content Inside the Div Element

I have a div with text that needs to be matched with comma-separated values: <div class="val"> This is paragraph 1 </div> <div class="val"> This is paragraph 2 </div> Using an Ajax call, I retrieve the ...

Make sure to tick the checkbox by clicking on the <tr> element using jQuery

I have the following HTML code snippet: <table class="table table-striped"> <thead> <tr> <th></th> <th>Color</th> </tr> </thead> <tbody> ...

What is the best way to swap values in an array with values from a different array?

Currently, I am retrieving an array: $sql = SELECT id, name, state FROM table ORDER BY name $result = mysqli_query($conn, $sql); $rows = array(); $dict = ["A","B","C"]; while ($row = mysqli_fetch_array($result)) { //replace state value here before next li ...

verifying the AJAX validation request URL

I am seeking a way to ensure that the valuable data generated by my ajax code on my webpage can only be accessed when the request originates from my site. This will prevent third parties from using the data without permission. I intend to implement this ...

Chosen item from the list

I am attempting to display the selected value from a list if it matches. While the list is successfully populated, the code handling the selected value does not execute. Code: $StaffName = 'Jimmy Chan'; <select name="Staff" id="Staff">< ...

How to enable drag-and-drop functionality for an iframe?

I've successfully made a chat widget draggable using react-draggable. However, the chat widget is also consumed by an iframe created entirely with HTML. I need the iframe to be draggable as well, but react-draggable doesn't support this. Are ther ...

How to efficiently retrieve individual values from input fields using jQuery AJAX while the inputs are within a foreach loop

In the world of PHP, input fields and delete buttons are magically generated by a foreach loop! To successfully extract the value from an input field and dispatch it to PHP, I rely on this code snippet: HTML: <input type="hidden" name="file_id" id="&l ...

What is the best way to create a Laravel object for use in JavaScript in a Laravel Blade?

please add description hereI am looking to obtain an object with this particular style var zoz2= { "11-30--0001": "<a href=\"https:\/\/www.dooz.ps\/p\/107229\" >\u0625\u0637\u0644\u0627& ...

Retrieve the names from one table by querying another table

I am working with a database that consists of 3 tables: Categories, Subcategories, and Articles. Within my Articles table, I have columns labeled CategoryID and SubcategoryID, both of which are foreign keys referencing the Categories and Subcategories tabl ...