简体   繁体   中英

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' ); ?> <?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 .

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.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM