Replace Woocommerce coupon amount with custom string

currently on the watch page and cart page after people apply the coupon code, and then the cart showing the coupon name and the amount of money reduced from the price of the original product.

For example, the price of a product is 100, and the discount is 20% per coupon, then it shows the name of the coupon, -20.

But I do not want to show -20 there, instead I need to display the user string there, for example, by 20%. Not a quantity, but some kind of user line.

How can I do this ?, When I search, I can find this topic /woocommerce/cart/cart.php, the function is used there <?php do_action( 'woocommerce_cart_collaterals' ); ?>. Thus, there is no way to edit the reduced amount.

So, please help, please note that this line should be displayed in the cash desk, basket, order email, etc.

+4
source share
1 answer

You need to use a filter hook woocommerce_cart_totals_coupon_html, as 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;
}

The code goes in the function.php file of the active child theme (or active theme). Tested and working.

enter image description here

It will replace the discounted amount with the coupon percentage on the pages of the basket and statement ...

+1
source

Source: https://habr.com/ru/post/1695648/


All Articles