Another day, another snippet! This snippet allows you to replace all occurrences of the coupon word to something that you like better, like “discount code”.
This code is highly inspired from the code shared in WPBeaches’ blog post. It’s customized for usage in classes, and optimized as some occurrences were not replaced and the “Click here to enter your code” was broken. I shared these optimizations here as I was not able to comment on the blog post.
The hooks, and filters we’re using
add_filter( 'gettext', array( $this, 'rename_coupon_field_on_cart' ), 10, 3 );
add_filter( 'woocommerce_coupon_error', array( $this, 'rename_coupon_label' ), 10, 3 );
add_filter( 'woocommerce_coupon_message', array( $this, 'rename_coupon_label' ), 10, 3 );
add_filter( 'woocommerce_cart_totals_coupon_label', array( $this, 'rename_coupon_label' ), 10, 1 );
add_filter( 'woocommerce_checkout_coupon_message', array( $this, 'rename_coupon_message_on_checkout' ) );
The functions
And these are the attached functions, mind that this code belongs in a class. A full code sample is shown in the bottom of this post.
public function rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {
if ( is_admin() || 'woocommerce' !== $text_domain ) {
return $translated_text;
}
if ( 'Coupon:' === $text ) {
$translated_text = 'Discount Code:';
}
if ( 'Coupon has been removed.' === $text ) {
$translated_text = 'Discount code has been removed.';
}
if ( 'Apply coupon' === $text ) {
$translated_text = 'Apply';
}
if ( 'Coupon code' === $text ) {
$translated_text = 'Discount Code';
}
if ( 'If you have a coupon code, please apply it below.' === $text ) {
$translated_text = 'If you have a discount code, please apply it below.';
}
return $translated_text;
}
public function rename_coupon_message_on_checkout( $text ) {
return 'Have a discount code?' . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'woocommerce' ) . '</a>';
}
public function rename_coupon_label( $err, $err_code = null, $something = null ) {
$err = str_ireplace( 'Coupon', 'Discount Code ', $err );
return $err;
}
Full code sample
<?php
namespace MK\Woocommerce;
class Rename_Coupon_Code {
public function __construct() {
$this->init();
}
public function init() {
add_filter( 'gettext', array( $this, 'rename_coupon_field_on_cart' ), 10, 3 );
add_filter( 'woocommerce_coupon_error', array( $this, 'rename_coupon_label' ), 10, 3 );
add_filter( 'woocommerce_coupon_message', array( $this, 'rename_coupon_label' ), 10, 3 );
add_filter( 'woocommerce_cart_totals_coupon_label', array( $this, 'rename_coupon_label' ), 10, 1 );
add_filter( 'woocommerce_checkout_coupon_message', array( $this, 'rename_coupon_message_on_checkout' ) );
}
public function rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {
if ( is_admin() || 'woocommerce' !== $text_domain ) {
return $translated_text;
}
if ( 'Coupon:' === $text ) {
$translated_text = 'Discount Code:';
}
if ( 'Coupon has been removed.' === $text ) {
$translated_text = 'Discount code has been removed.';
}
if ( 'Apply coupon' === $text ) {
$translated_text = 'Apply';
}
if ( 'Coupon code' === $text ) {
$translated_text = 'Discount Code';
}
if ( 'If you have a coupon code, please apply it below.' === $text ) {
$translated_text = 'If you have a discount code, please apply it below.';
}
return $translated_text;
}
public function rename_coupon_message_on_checkout( $text ) {
return 'Have a discount code?' . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'woocommerce' ) . '</a>';
}
public function rename_coupon_label( $err, $err_code = null, $something = null ) {
$err = str_ireplace( 'Coupon', 'Discount Code ', $err );
return $err;
}
}
michaelfinch1254 says
Excellent blog on coupon code text to discount code in woocommerce, I really apprciate. Please keep sharing.
Kai says
where do i nedd to rename? i can`t find a file calls functions.php in woocommerce folder.