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 *

Ready to take action?

I'm looking forward to discuss your projects and goals!
Feel free to reach out using the contact details below.

Marinus Klasen

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

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Storing private data with SSH on WPEngine sitesDecember 15, 2022
  • Get the HTML content of a block edit page or post in WordPressNovember 30, 2022
  • Rename Coupon code text to Discount code in WoocommerceNovember 3, 2022
  • Background-size cover in mPDFOctober 22, 2022
  • WordPress.com SSH & duplicating a WordPress.com website locallyOctober 11, 2022

Copyright © 2023 · Marinus Klasen | Webdesign by Team Rood