The default behavior of a Woocommerce AddToCart Button is to first show "Add to Cart". Once clicked, it will go to a Loading State and checks the AJAX request, then shows "in cart {number}".
I couldn’t find any action/filter to hook into. I can change the "Add to Cart" text in many different ways: hooks, translations, CSS, etc. But for the latter state, JavaScript is in charge, and, as a result even translation doesn’t work.
While I very much like this functionality, I have to find a way to replace "in cart" with an icon while keeping the {number} part.
Please let me know if you have robust and clean way of doing this.
Thank you in advance.
I tried reproducing the very structure of the HTML, even the wp-data attributes, but I couldn’t get far, since after replacing this with the default woocommerce html, the interactivity is gone.
I’m not that good at Javascript, but as ugly as it seems, I even tried to change it with a script that looks for the addedtocart event, and changes the text. In vain…
The obvious way is to create a customized button from scratch but I won’t be able to do it at this moment because I have other requirements to deliver.
I also tried whatever Chatgpt had to offer with its vast knowledge, to no success.
3
Answers
A rather clean solution I found:
I had tested translation, but I was expecting "%d in cart" which was wrong and I thought that's out of question. With "%s in cart" it works. I removed the "in cart" text and added the icon using a ::before psuedo-element.
You can write custom javascript to do this
When the button text changes to "in cart {number}", dynamically replace the text "in cart" with your desired icon (e.g., using HTML or a FontAwesome icon).
you can write the javascript code in custom.js or function.php script file of your theme
Here is the simple .js code which you can paste in custom.js in your theme
here how you add that .js function to your website
Can you try this modified version? Its working here in my system.
place the following code lines in custom-add-to-cart.js