Custom html for fee in WooCommerce checkout

113 Views Asked by At

I would like you to ask, how can I add custom html for fee I am adding dynamically to the order?

I am doing this way:

add_action( 'woocommerce_cart_calculate_fees', 'apply_discount_for_subscribers', 20, 1 );

function apply_discount_for_subscribers( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    if ( is_user_logged_in() ) {
        $list_id = 3; // ID

        // Sprawdzenie subskrypcji
        $subscription_status = check_subscription_status( $list_id );
        if ( $subscription_status === 'subscribed' ) {

            $label = '5% discount:';
            $discount = $cart->get_subtotal() * 0.05; // 5% zniżki

            $cart->add_fee( $label, -$discount, true, 'standard' );
        }
    }
}

But what I receive is:

5% rabatu dla subskrybentów:
<span class="woocommerce-Price-amount amount">
    <bdi>-12,50&nbsp;
        <span class="woocommerce-Price-currencySymbol">zł</span>
    </bdi>
</span>

Basically it would be enough to just wrap this in div, but I cannot not achieve it:

<div>
    5% rabatu dla subskrybentów:
    <span class="woocommerce-Price-amount amount">
        <bdi>-12,50&nbsp;
            <span class="woocommerce-Price-currencySymbol">zł</span>
        </bdi>
    </span>
</div>

BTW. why add_fee does not display as defined in review-order.php

<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
    <tr class="fee">
        <th><?php echo esc_html( $fee->name ); ?></th>
        <td><?php wc_cart_totals_fee_html( $fee ); ?></td>
    </tr>
<?php endforeach; ?>

Please help! :)

1

There are 1 best solutions below

3
Bhautik On

You can wrap div like $label = '<div>5% discount:</div>'; but as you can see in WC()->cart->get_fees() loop label code use esc_html so it will not work.

So You can override the review-order.php template file in your theme. follow the below steps.

  1. Create a new folder in your active theme directory called woocommerce.
  2. Inside the woocommerce folder, create another folder called checkout.
  3. Copy the review-order.php file from the wp-content/plugins/woocommerce/templates/checkout directory to your newly created woocommerce/checkout folder within your theme.
  4. Open the copied review-order.php file.

Change this code

<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
    <tr class="fee">
        <th><?php echo esc_html( $fee->name ); ?></th>
        <td><?php wc_cart_totals_fee_html( $fee ); ?></td>
    </tr>
<?php endforeach; ?>

to this and add a condition based on your fee label so it only applies to that particular fee 5% discount: unless you don't want to apply for all fees.

<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
    <tr class="fee">
        <th>
            <?php if ( $fee->name === '5% discount:' ) : ?>
                <div><?php echo esc_html( $fee->name ); ?></div>
            <?php }else{ ?>
                <?php echo esc_html( $fee->name ); ?>
            <?php } ?>
        </th>
        <td><?php wc_cart_totals_fee_html( $fee ); ?></td>
    </tr>
<?php endforeach; ?>