Home / eCommerce / Rename WooCommerce “Coupon” terminology to “Discount”
Duplicate Snippet

Embed Snippet on Your Site

Rename WooCommerce “Coupon” terminology to “Discount”

# Works across front-end (cart, checkout, Woo alerts/notifications) as well as email templates but keeps admin backend untouched.
# Works seamlessly with separate snippet that modifies coupon code entry field placeholder and apply button text which has a lower priority in snippet (same default '10' WPCode Snippet priority).

Code Preview
php
<?php
if ( class_exists( 'WooCommerce' ) ) {
    add_filter( 'gettext', 'rd_wc_generic_coupon_replacement', 20, 3 );
    add_filter( 'ngettext', 'rd_wc_generic_coupon_replacement_plural', 20, 5 );
    // Also replace wording directly in notices (covers cases where text bypasses gettext later)
    add_filter( 'woocommerce_add_notice', 'rd_wc_generic_coupon_replacement_in_notice', 20, 1 );
    add_filter( 'woocommerce_add_success', 'rd_wc_generic_coupon_replacement_in_notice', 20, 1 );
    add_filter( 'woocommerce_add_error', 'rd_wc_generic_coupon_replacement_in_notice', 20, 1 );
    function rd_wc_apply_coupon_discount_replacements( $text ) {
        if ( ! is_string( $text ) || $text === '' ) {
            return $text;
        }
        // Phrase-level fixes first (prevents "discount discounts" headings and preserves case)
        $phrase_replace = array(
            // "coupon discount(s)" variants
            'COUPON DISCOUNT'     => 'DISCOUNT',
            'Coupon discount'     => 'Discount',
            'coupon discount'     => 'discount',
            'COUPON DISCOUNTS'    => 'DISCOUNTS',
            'Coupon discounts'    => 'Discounts',
            'coupon discounts'    => 'discounts',
            // "discount coupon(s)" variants – preserve case
            'DISCOUNT COUPONS'    => 'DISCOUNTS',
            'Discount Coupons'    => 'Discounts',
            'Discount coupons'    => 'Discounts',
            'discount coupons'    => 'discounts',
            // If something has already been partially replaced
            'DISCOUNT DISCOUNTS'  => 'DISCOUNTS',
            'Discount discounts'  => 'Discounts',
            'discount discounts'  => 'discounts',
        );
        $text = str_replace(
            array_keys( $phrase_replace ),
            array_values( $phrase_replace ),
            $text
        );
        $search_replace = array(
            // Theme-specific checkout wording
            'Have A Promotional Code'  => 'Got a Discount Code?',
            'have a promotional code'  => 'Got a Discount Code?',
            'HAVE A PROMOTIONAL CODE'  => 'Got a Discount Code?',
            // Composite phrases
            'COUPON CODE'        => 'DISCOUNT CODE',
            'Coupon Code'        => 'Discount Code',
            'Coupon code'        => 'Discount code',
            'coupon code'        => 'discount code',
            'PROMOTIONAL CODE'   => 'DISCOUNT CODE',
            'Promotional Code'   => 'Discount Code',
            'Promotional code'   => 'Discount code',
            'promotional code'   => 'discount code',
            // Plural
            'COUPONS'            => 'DISCOUNTS',
            'Coupons'            => 'Discounts',
            'coupons'            => 'discounts',
            // Singular
            'COUPON'             => 'DISCOUNT',
            'Coupon'             => 'Discount',
            'coupon'             => 'discount',
        );
        $text = str_replace(
            array_keys( $search_replace ),
            array_values( $search_replace ),
            $text
        );
        // Final cleanup for any duplicates introduced elsewhere (case-preserving)
        $text = preg_replace( '/\b(Discounts)\s+\1\b/', '$1', $text );
        $text = preg_replace( '/\b(discounts)\s+\1\b/', '$1', $text );
        $text = preg_replace( '/\b(DISCOUNTS)\s+\1\b/', '$1', $text );
        $text = preg_replace( '/\b(Discount)\s+\1\b/', '$1', $text );
        $text = preg_replace( '/\b(discount)\s+\1\b/', '$1', $text );
        $text = preg_replace( '/\b(DISCOUNT)\s+\1\b/', '$1', $text );
        return $text;
    }
    function rd_wc_generic_coupon_replacement( $translated, $original, $domain ) {
        if ( is_admin() ) {
            return $translated;
        }
        return rd_wc_apply_coupon_discount_replacements( $translated );
    }
    function rd_wc_generic_coupon_replacement_plural( $translated, $single, $plural, $number, $domain ) {
        if ( is_admin() ) {
            return $translated;
        }
        return rd_wc_apply_coupon_discount_replacements( $translated );
    }
    function rd_wc_generic_coupon_replacement_in_notice( $message ) {
        if ( is_admin() ) {
            return $message;
        }
        return rd_wc_apply_coupon_discount_replacements( $message );
    }
}

Comments

Add a Comment