WooCommerce:允许在“add_fee 函数"中使用 HTML 标签;标签 [英] WooCommerce: Allow HTML tags in "add_fee function" label

查看:24
本文介绍了WooCommerce:允许在“add_fee 函数"中使用 HTML 标签;标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码根据付款方式添加自定义费用:

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

我想使用 HTML 在标签旁边显示一个图标,如下所示:

 $label = 'PayPal 费用 <span>HTML</span>';

然而,字符串将被 (esc_html) 转义为 HTML 标签.如何让它与 HTML 标签一起显示我的图标?

解决方案

另一种选择是将模板从 woocommerce/templates 目录复制到您的主题(或子主题):

  • 结帐/review-order.php
  • cart/cart-totals.php

在这些文件中,您将在 esc_html 函数所在的位置找到此代码,并根据需要对其进行

 <?php foreach ( WC()->cart->get_fees() as $fee ) : ?><th><?php echo esc_html( $fee->name );?></th><td data-title="<?php echo esc_attr( $fee->name );/* 留下这个 */?>>>><?php wc_cart_totals_fee_html( $fee );?></td></tr><?php endforeach;?>

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?

解决方案

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; ?>

这篇关于WooCommerce:允许在“add_fee 函数"中使用 HTML 标签;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆