Search code examples
phpwordpresswoocommercehook-woocommercecoupon

Replace Woocommerce coupon amount line by a custom string


currently in checkout page and cart page after people apply coupon code then cart showing coupon name and how much money reduced from original product price .

For example product price is 100 and discount is 20% for coupon then it showing coupon name , -20 .

But i don't want to show -20 there , instead i need to display custom string there like 20% off . Not the amount , but some custom string ..

How i can do this ? . When i search i can find that theme/woocommerce/cart/cart.php there it is using a function <?php do_action( 'woocommerce_cart_collaterals' ); ?> . So there is no option for to edit the reduced amount .

So please help , please note that this string need to displayed in checkout , cart , order email etc .


Solution

  • You need to use woocommerce_cart_totals_coupon_html filter hook like in this example:

    add_filter( 'woocommerce_cart_totals_coupon_html', 'custom_cart_totals_coupon_html', 30, 3 );
    function custom_cart_totals_coupon_html( $coupon_html, $coupon, $discount_amount_html ) {
        // For percent coupon types only
        if( 'percent' == $coupon->get_discount_type() ){
            $percent              = $coupon->get_amount(); // Get the coupon percentage number
            $discount_amount_html = '<span>' . $percent . ' % </span>'; // Formatting  percentage
            // Replacing coupon discount, by custom percentage
            $coupon_html          = $discount_amount_html . ' <a href="' . esc_url( add_query_arg( 'remove_coupon', urlencode( $coupon->get_code() ), defined( 'WOOCOMMERCE_CHECKOUT' ) ? wc_get_checkout_url() : wc_get_cart_url() ) ) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr( $coupon->get_code() ) . '">' . __( '[Remove]', 'woocommerce' ) . '</a>';
        }
    
        return $coupon_html;
    }
    

    Code goes in function.php file of the active child theme (or active theme). Tested and works.

    enter image description here

    It will replace the discounted amount by the coupon percentage on cart and checkout pages…