Support » Plugin: WooCommerce » Change coupon error message using functions.php

  • I’ve researched on woocommerce forums and docs, but only found a snippet that didn’t make any sense for what I’m trying to do.

    I would like to change one of the coupon error messages by adding some code to the theme’s functions.php file.

    The code I found on their forums is this:

    add_action( 'init', 'woocommerce_custom_message' );
    function woocommerce_custom_message() {
    global $woocommerce;
    $woocommerce->add_message( 'My Message' );
    $woocommerce->add_error( 'An Error Message' );

    But that code doesn’t mention any way to target a specific error message. In the core woocommerce files, there’s a file named ‘class-wc-coupon.php’ which has code in it pertaining to that message, which reads like this (only posting the one message here, not all):

    public function get_coupon_error( $err_code ) {
    switch ( $err_code ) {
    				$err = sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), woocommerce_price( $this->minimum_amount ) );
    				$err = '';
    		return apply_filters( 'woocommerce_coupon_error', $err, $err_code, $this );

    Does anyone have any idea how to format this for the functions.php file so it overrides the core? I don’t want to have to update the core file every time the plugin is updated, if possible.


Viewing 1 replies (of 1 total)
  • You can do it with a get text filter

    function custom_coupon_messages( $translated_text, $text, $text_domain ) {
    	// bail if not modifying frontend woocommerce text
    	if ( is_admin() || 'woocommerce' !== $text_domain ) {
    		return $translated_text;
    	if ( 'Coupon usage limit has been reached.' === $text ) {
    		return 'Our records show that you have already redeemed this discount code.';
    	if( 'This coupon has expired.' === $text ) {
    		return 'Sorry, this discount code is no longer active.';
    	return $translated_text;
    add_filter( 'gettext', 'custom_coupon_messages', 10, 3 );
Viewing 1 replies (of 1 total)
  • The topic ‘Change coupon error message using functions.php’ is closed to new replies.