skip to Main Content

I’m using the following code to add a custom fee based on payment method:

function stackoverflow_apply_payment_gateway_fee() {
 $payment_method = WC()->session->get( 'chosen_payment_method' );
 if( $payment_method == 'paypal' ) {
 $label =  'PayPal Fee <span>HTML</span>';
 $cart_subtotal_fee = WC()->cart->get_subtotal();
 $amount = 5;
 WC()->cart->add_fee( $label, $amount, true, 'standard' );
 }
}
add_action( 'woocommerce_cart_calculate_fees', 'stackoverflow_apply_payment_gateway_fee' );

I want to show an icon next to the label using HTML, like this:

     $label =  'PayPal Fee <span>HTML</span>';

However string will be (esc_html) escaped for HTML tags. How can I make it work with HTML tags to show my icon?

2

Answers


  1. There is a filter hook called esc_html in WP esc_html function that you can use. you can append your icon HTML to your label. check below code. code will go in your active theme functions.php file.

    function stackoverflow_apply_payment_gateway_fee() {
        $payment_method = WC()->session->get( 'chosen_payment_method' );
        if( $payment_method == 'paypal' ) {
            $label             =  'PayPal Fee';
            $cart_subtotal_fee = WC()->cart->get_subtotal();
            $amount            = 5;
            WC()->cart->add_fee( $label, $amount, true, 'standard' );
        }
    }
    add_action( 'woocommerce_cart_calculate_fees', 'stackoverflow_apply_payment_gateway_fee' );
    
    function show_an_icon_next_to_the_paypal_fee_label( $safe_text, $text ){
        if( ( is_cart() || is_checkout() ) && $text == 'PayPal Fee' ){
            $safe_text = $safe_text.'<span>HTML</span>';
        }
        return $safe_text;
    }
    add_filter( 'esc_html', 'show_an_icon_next_to_the_paypal_fee_label', 10, 2 );
    
    Login or Signup to reply.
  2. Another alternative would be to copy the templates over to your theme (or child theme) from the woocommerce/templates directory:

    • checkout/review-order.php
    • cart/cart-totals.php

    In those files wou will find this code where the esc_html function is, and edit it as you need:

        <?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
            <tr class="fee">
                <th><?php echo esc_html( $fee->name ); ?></th>
                <td data-title="<?php echo esc_attr( $fee->name ); /* Leave this one */?>"><?php wc_cart_totals_fee_html( $fee ); ?></td>
            </tr>
        <?php endforeach; ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search