简体   繁体   English

如何在PHP中的文本上添加FontAwesome图标?

[英]How to add FontAwesome icon instead on text in php?

I'm trying replace string "Add to cart" with cart's icon. 我正在尝试用购物车的图标替换字符串“添加到购物车”。 The question is how to insert font awesome icon <i class="fa fa-shopping-cart" aria-hidden="true"></i> inside this php code: 问题是如何在此php代码内插入字体超赞图标<i class="fa fa-shopping-cart" aria-hidden="true"></i>

$text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add to cart', 'woocommerce' ) : __( 'Read more', 'woocommerce' );

return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );

Thanks in advance 提前致谢

St Pavel, 圣帕维尔

Not familiar with woocommerce but... 不熟悉woocommerce但...

Did you try: 你试过了吗:

$text = $this->is_purchasable() && $this->is_in_stock() ? __( '<i class="fa fa-shopping-cart" aria-hidden="true"></i>', 'woocommerce' ) : __( 'Read more', 'woocommerce' );

EDIT: 编辑:

From your comment, it looks like apply_filters function does not accept HTML value as an arg, for it's second param. 根据您的评论,apply_filters函数似乎不接受HTML值作为arg,因为它是第二个参数。 As such, here's what I recommend: 因此,这是我的建议:

Most simplest approach is to use jQuery: 最简单的方法是使用jQuery:

I looked at the demo here: https://demo.woothemes.com/storefront/shop/ inspected the "Add to cart" button... as far as <a> tag's class goes, the value is: class="button product_type_simple add_to_cart_button ajax_add_to_cart fa fa-cart-plus" 我在此处查看了该演示: https : //demo.woothemes.com/storefront/shop/检查了“添加到购物车”按钮...就<a>标签的类而言,其值为: class="button product_type_simple add_to_cart_button ajax_add_to_cart fa fa-cart-plus"

As such you can do either: 因此,您可以执行以下任一操作:

jQuery('a.ajax_add_to_cart').addClass('fa fa-cart-plus');

or 要么

jQuery('a.add_to_cart_button').addClass('fa fa-cart-plus');

EDIT #2: 编辑#2:

And since you are trying to replace the text with the icon (and not have icon AND text), do the following: 并且由于您尝试用图标替换文本(并且没有图标AND文本),因此请执行以下操作:

jQuery('a.ajax_add_to_cart').text('');

or 要么

jQuery('a.add_to_cart_button').text('');

Hope this helps! 希望这可以帮助!

-Rush -拉什

woocommerce/loop/add-to-cart.php woocommerce / loop / add-to-cart.php

replace this 取代这个

sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">
    <i class="fa fa-shopping-cart"></i>&nbsp;&nbsp;%s</a>',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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