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

This site runs on Cloudways.
It's fast isn't it?
Cloudways offers high-quality, fast and affordable hosting.

Learn more

Need a hand? Post your project and hire me and other top notch developers on Codeable.

Hire me on Codeable

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

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

[email protected]
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Disable the ‘Password Changed’ Email notification in WordPress with a filterAugust 30, 2023
  • A-Frame: Change an objects color on hoverAugust 24, 2023
  • Connecting to a Cloudways application with VSCodeAugust 15, 2023
  • Force refresh PHP-FPM or Litespeed cacheAugust 4, 2023
  • Only return zip/postal codes from Google Places AutoComplete Javascript APIJuly 19, 2023

Copyright © 2023 · Marinus Klasen | Webdesign by Team Rood