Skip to content

Instantly share code, notes, and snippets.

@maxrice
Last active July 5, 2023 19:34
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxrice/8551024 to your computer and use it in GitHub Desktop.
Save maxrice/8551024 to your computer and use it in GitHub Desktop.
WooCommerce - rename the "Have a Coupon?" message and "Apply Coupon" field on the checkout
<?php
// rename the "Have a Coupon?" message on the checkout page
function woocommerce_rename_coupon_message_on_checkout() {
return 'Have a Promo Code?' . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'woocommerce' ) . '</a>';
}
add_filter( 'woocommerce_checkout_coupon_message', 'woocommerce_rename_coupon_message_on_checkout' );
// rename the coupon field on the checkout page
function woocommerce_rename_coupon_field_on_checkout( $translated_text, $text, $text_domain ) {
// bail if not modifying frontend woocommerce text
if ( is_admin() || 'woocommerce' !== $text_domain ) {
return $translated_text;
}
if ( 'Coupon code' === $text ) {
$translated_text = 'Promo Code';
} elseif ( 'Apply Coupon' === $text ) {
$translated_text = 'Apply Promo Code';
}
return $translated_text;
}
add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_checkout', 10, 3 );
@foamymedia
Copy link

foamymedia commented Sep 7, 2018

you can just edit the woocommerce/checkout/form-coupon.php file and add this to your theme

@gabros20
Copy link

gabros20 commented Apr 7, 2019

In case someone want's to change the "If you have a coupon code, please apply it below." string to something else, here is a way to do it.

add_filter( 'gettext', 'woocommerce_change_coupon_field_instruction_text' );

function woocommerce_change_coupon_field_instruction_text($translated) {
$translated = str_ireplace('If you have a coupon code, please apply it below.', 'YOUR NEW STRING', $translated);
return $translated;
}

@jthomae1
Copy link

@gabros20 anything changed here? That's not working for me so far

@Ruben2600
Copy link

Hello! Can I change also the background off the promo code? Is now #FFFFFF I want to change it! THanks!

@sajjadalis
Copy link

This is not working for me as well.

@Ruben2600 try to change with css.

.cart .coupon .button {
    background-color: #000000;
}

replace it with proper css class (applied in your theme).

@rtpHarry
Copy link

I just went through this and this code snippet is working for me:

/**
 * Change 'coupon' text to 'voucher'
 * @source https://gist.github.com/maxrice/8551024
 */
function bt_rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {
	// bail if not modifying frontend woocommerce text
	if ( is_admin() || 'woocommerce' !== $text_domain ) {
		return $translated_text;
	}
	if ( 'Coupon:' === $text ) {
		$translated_text = 'Voucher Code:';
	}

	if ('Coupon has been removed.' === $text){
		$translated_text = 'Voucher code has been removed.';
	}

	if ( 'Apply coupon' === $text ) {
		$translated_text = 'Apply voucher';
	}

	if ( 'Coupon code' === $text ) {
		$translated_text = 'Voucher code';
	
	} 

	return $translated_text;
}
add_filter( 'gettext', 'bt_rename_coupon_field_on_cart', 10, 3 );

/**
 * Change 'coupon' text to 'voucher'
 * @source https://gist.github.com/maxrice/8551024
 */
function bt_rename_coupon_message_on_checkout() {
	return 'Have a voucher code?' . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'woocommerce' ) . '</a>.';
}
add_filter( 'woocommerce_checkout_coupon_message', 'bt_rename_coupon_message_on_checkout' );

/**
 * Change 'coupon' text to 'voucher'
 * @source https://gist.github.com/maxrice/8551024
 */
function bt_rename_coupon_label( $err, $err_code=null, $something=null ){
	$err = str_ireplace("Coupon","voucher",$err);
	return $err;
}
add_filter( 'woocommerce_coupon_error', 'bt_rename_coupon_label', 10, 3 );
add_filter( 'woocommerce_coupon_message', 'bt_rename_coupon_label', 10, 3 );
add_filter( 'woocommerce_cart_totals_coupon_label', 'bt_rename_coupon_label',10, 1 );

Just posting it for easy reference for future developers, as it compiles others feedback in this thread, plus fixes missing bits of html in one of the comments.

@rtpHarry
Copy link

Actually I've tweaked it to the following code now, just to catch a couple more edge cases:

/**
 * Soka - Change 'coupon' text to 'voucher'
 * @source https://gist.github.com/maxrice/8551024
 */
function soka_rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {
	// bail if not modifying frontend woocommerce text
	if ( is_admin() || 'woocommerce' !== $text_domain ) {
		return $translated_text;
	}
	if ( 'Coupon:' === $text ) {
		$translated_text = 'Voucher Code:';
	}

	if ('Coupon has been removed.' === $text){
		$translated_text = 'Voucher code has been removed.';
	}

	if ( 'Apply coupon' === $text ) {
		$translated_text = 'Redeem voucher';
	}

	if ( 'Coupon code' === $text ) {
		$translated_text = 'Voucher code';
	} 

	if ( 'If you have a coupon code, please apply it below.' === $text ) {
		$translated_text = 'If you have a voucher code, please apply it below.';
	} 

	return $translated_text;
}
add_filter( 'gettext', 'soka_rename_coupon_field_on_cart', 10, 3 );

/**
 * Soka - Change 'coupon' text to 'voucher'
 * @source https://gist.github.com/maxrice/8551024
 */
function soka_rename_coupon_message_on_checkout() {
	return 'Have a voucher code?' . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'woocommerce' ) . '</a>.';
}
add_filter( 'woocommerce_checkout_coupon_message', 'soka_rename_coupon_message_on_checkout' );

/**
 * Soka - Change 'coupon' text to 'voucher'
 * @source https://gist.github.com/maxrice/8551024
 */
function soka_rename_coupon_label( $err, $err_code=null, $something=null ){
	$err = str_replace("Coupon","Voucher",$err);
	$err = str_replace("coupon","voucher",$err);
	return $err;
}
add_filter( 'woocommerce_coupon_error', 'soka_rename_coupon_label', 10, 3 );
add_filter( 'woocommerce_coupon_message', 'soka_rename_coupon_label', 10, 3 );
add_filter( 'woocommerce_cart_totals_coupon_label', 'soka_rename_coupon_label',10, 1 );

@RubienRe
Copy link

RubienRe commented Jun 4, 2023

image
Bit late to the party, but is there a way to just hide / remove: 'Have a coupon? [Click here to enter your code]' and 'Coupon:'? I already made it so it automatically opens whenever you enter the checkout page. Thanks! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment