WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Change coupon error message using functions.php (2 posts)

  1. Treebeard
    Member
    Posted 10 months ago #

    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 ) {
    case self::E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET:
    				$err = sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), woocommerce_price( $this->minimum_amount ) );
    			break;
    default:
    				$err = '';
    			break;
    		}
    
    		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.

    Thanks!

    http://wordpress.org/plugins/woocommerce/

  2. defunctlife
    Member
    Posted 1 month ago #

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

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.