In this guide, I’ll share a simple code that you can copy and paste to your WooCommerce store to change the apply coupon button text. You can change the text to anything you want. I’ll also explain each line of code for your better understanding.
So, without any further ado, let’s straight jump into the tutorial.
On This Page
How to Change the Apply Coupon Button Text in WooCommerce?
By default, the WooCommerce coupon button has the ‘Apply Coupon’ text. It is self-explanatory but you may want to change it to match your brand or make it more user-friendly.
In that case, you can follow the following steps.
1. Code to Change the ‘Apply Coupon’ Button Text in WooCommerce
Here’s the code to change the apply coupon button text. Ensure to replace the ‘Your New Button Text’ with the text you want to display instead of the default ‘Apply Coupon’ text.
function aovup_custom_woocommerce_button_text($translation, $text, $domain) {
if ($domain == 'woocommerce' && $text == 'Apply coupon') {
return 'Your New Button Text'; // Replace 'Your New Button Text' with the desired text.
}
return $translation;
}
add_filter('gettext', 'aovup_custom_woocommerce_button_text', 10, 3);
2. Code Explanation
Here’s a detailed explanation of the code:
2.1. Defining the Function
function aovup_custom_woocommerce_button_text($translation, $text, $domain) {
This line declares a new function named aovup_custom_woocommerce_button_text which takes three parameters: $translation, $text, and $domain.
- $translation: This is the translated text that’s currently being used.
- $text: This is the original text before being translated.
- $domain: This refers to the domain for the translation, often used to identify which plugin or theme the translation belongs to.
2.2. Conditional Check
if ($domain == 'woocommerce' && $text == 'Apply coupon') {
This line checks two conditions:
- If the $domain is ‘woocommerce’, which means that the translation is from the WooCommerce plugin.
- If the original text $text is ‘Apply coupon’.
Both conditions need to be true for the code inside the curly braces {…} to execute.
2.3. Returning New Text
return 'Your New Button Text';
If the conditions are met (i.e., the text is “Apply coupon” from WooCommerce), this line replaces the text with ‘Your New Button Text’. You’d replace ‘Your New Button Text’ with whatever you want the button to say.
2.4. Returning the Original Translation
return $translation;
If the conditions in the if statement aren’t met (i.e., it’s not the ‘Apply coupon’ text from WooCommerce), the original translation $translation is returned, ensuring other translations aren’t affected.
2.5. Adding the Function to a Filter Hook
add_filter('gettext', 'aovup_custom_woocommerce_button_text', 10, 3);
This line attaches the aovup_custom_woocommerce_button_text function to the gettext filter hook, which is a core WordPress hook used for text translations.
- ‘gettext’: The name of the filter hook.
- ‘aovup_custom_woocommerce_button_text’: The name of the function we want to attach.
- 10: Priority of the function, determining the order it will be executed if there are multiple functions attached to the gettext hook. In this case, 10 is a default priority.
- 3: Number of arguments the function accepts (in this case, $translation, $text, and $domain).
3. Paste the Code to Your WooCommerce Store
To paste the code to your store, there are multiple ways. You can use the one that suits you the best. I have used a plugin to paste the code to my demo store.
If you don’t know how to add a code snippet to your WordPress and WooCommerce store. Here’s a tutorial to help you with this:
4. Final Result
Let’s test the final results.
To test it, I visited my store’s cart page, and here’s the custom apply button text that we added above.
Conclusion
That’s it for today’s article.
I hope it was helpful to you and you were able to change the apply coupon button text in WooCommerce easily. It seems to be a small tweak or customization but it can greatly increase the customer experience.
Remember, in the vast landscape of e-commerce, it’s the attention to detail that often sets apart the best online stores. I recommend you explore other customizations as well.