Marinus Klasen

Marinus Klasen

  • Development
  • Consultancy
  • Developer Resources
  • Blog
  • Contact

November 3, 2022

Rename Coupon code text to Discount code in Woocommerce

Marinus Klasen

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;
	}
}

About Marinus Klasen

Marinus has been working in software/web development for more than a decade. Since 2020 his attention shifted on sharing knowledge and developing products and tools for sharing knowledge.

Marinus Klasen on Twitter

Looking for a WordPress expert?
Hire top notch developers on Codeable!

Consult an expert

Comments

  1. michaelfinch1254 says

    January 24, 2023 at 9:40 am

    Excellent blog on coupon code text to discount code in woocommerce, I really apprciate. Please keep sharing.

    Reply
  2. Kai says

    November 13, 2023 at 10:06 am

    where do i nedd to rename? i can`t find a file calls functions.php in woocommerce folder.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get in touch

Contact me using the details below

Marinus Klasen

Wilhelmina van Pruisenweg 35, 2595 AN Den Haag

marinus@mklasen.com
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Elementor Widget UsageMarch 27, 2025
  • Programmatically showing popups with ElementorMarch 25, 2025
  • The Elementor sanitize_settings errorSeptember 12, 2024
  • Announcing.. Wooping Shop Health!June 13, 2024
  • Swiper setup when using wp-scriptsMarch 14, 2024

Copyright © 2025 ยท Marinus Klasen | Webdesign by Team Rood