Customize Coupon Code Text in WooCommerce

In WooCommerce, the default text for the coupon code field might not always suit your store’s branding or messaging. In this post, we’ll learn how to customize the text “Coupon Code” to “Discount Code” throughout your WooCommerce store.

/**
* Snippet Name: Rename "Coupon Code" to "Discount Code" in WooCommerce
* Snippet Author: wpsnippets.dev
*/

//Modifies the text displayed for the coupon code field
function wps_customize_coupon_text_in_woocommerce( $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 Code';
    }
    if ( 'Coupon code' === $text ) {
        $translated_text = 'Discount Code';
    }
    return $translated_text;
}
add_filter( 'gettext', 'wps_customize_coupon_text_in_woocommerce', 10, 3 );

//Changes the message displayed on the checkout page
function wps_customize_coupon_message_on_checkout() {
    return 'Have a discount code? <a href="#" class="showcoupon">Enter it here</a>';
}
add_filter( 'woocommerce_checkout_coupon_message', 'wps_customize_coupon_message_on_checkout' );

//Removes the default instruction text prompting users to apply a coupon code
function wps_customize_coupon_field_instruction_text($translated) {
    $translated = str_ireplace('If you have a coupon code, please apply it below.', '', $translated);
    return $translated;
}
add_filter( 'gettext', 'wps_customize_coupon_field_instruction_text', 20 );

// Replace text "Coupon Code" with Discount Code
function wps_customize_coupon_error_message( $err, $err_code=null, $something=null ){
    $err = str_ireplace("Coupon","Discount ",$err);
    return $err;
}
add_filter( 'woocommerce_coupon_error', 'wps_customize_coupon_error_message', 10, 3 );

Code Explanation

  • Modifies the text displayed for the coupon code field
  • The function customize_coupon_code_text modifies the text displayed for the coupon code field. It uses the gettext filter to intercept and change the text strings. It checks if the current context is not in the admin area and the text domain is WooCommerce.
    • It replaces the “Coupon” text with “Discount”. For example, replace the “Coupon has been removed” text with “Discount code has been removed.” and the “Apply coupon” text with “Apply Discount” and “Coupon code” text with “Discount Code.”
  • Changes the message displayed on the checkout page
    • The function customize_coupon_message_on_checkout changes the message displayed on the checkout page, prompting users to enter a discount code. It returns a custom message with a link to reveal the coupon code field.
  • Removes the default instruction text prompting users to apply a coupon code
    • The function remove_coupon_field_instruction_text removes the default instruction text prompting users to apply a coupon code. It uses the gettext filter to replace the instruction text with an empty string.
  • Modifies error and success messages
    • The function customize_coupon_label modifies error and success messages related to coupons. It replaces instances of “Coupon” with “Discount” to maintain consistency with the updated terminology.
    • These functions work together to ensure that the text “Coupon Code” is consistently replaced with “Discount Code” throughout the WooCommerce store, providing a cohesive and branded user experience.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top