Php 在Woocommerce归档页面上的产品价格后添加图标
我正在尝试在Woocommerce中的产品价格之后添加一个自定义图标,用于商店页面上的特定产品类别。因此,我想在“fast SHIPPING”产品类别的所有产品的价格之后添加一个“fast delivery truck”图标 我希望它能在wish.com网站上显示,如以下截图所示: 这就是我尝试过的:Php 在Woocommerce归档页面上的产品价格后添加图标,php,css,wordpress,woocommerce,icons,Php,Css,Wordpress,Woocommerce,Icons,我正在尝试在Woocommerce中的产品价格之后添加一个自定义图标,用于商店页面上的特定产品类别。因此,我想在“fast SHIPPING”产品类别的所有产品的价格之后添加一个“fast delivery truck”图标 我希望它能在wish.com网站上显示,如以下截图所示: 这就是我尝试过的: add_filter( 'woocommerce_price_html', 'prepend_append_icon_to_price', 10, 2 ); function prepend_a
add_filter( 'woocommerce_price_html', 'prepend_append_icon_to_price', 10, 2 );
function prepend_append_icon_to_price( $price, $instance ) {
if(is_product_category( 'fast-shipping')){
$icon = ' <i class="fas fa-shipping-fast"></i> ';
$price = $icon . $price . $icon;
}
return $price;
}
add_filter('woocommerce_price_html','prepend_append_icon_to_price',10,2);
函数prepend\u将\u图标\u追加到\u价格($price,$instance){
如果(是产品类别(‘快速装运’){
$icon='';
$price=$icon.$price.$icon;
}
返回$price;
}
但它没有显示任何价格后
任何帮助都将不胜感激。在属于价格标签之前或之后使用css:
fa-shipping-fast:before{
content: url(.....);
width : ....;
height : ....;
....
}
自从Woocommerce 3以来,您使用了错误的钩子,代码中有一些错误。 要在右侧价格后显示图标,请在两种情况下显示“快速发货”产品类别的图标: 1) 在所有Woocommerce归档页面上:
add_filter( 'woocommerce_get_price_html', 'prepend_append_icon_to_price', 10, 2 );
function prepend_append_icon_to_price( $price, $product ) {
if( has_term( 'fast-shipping', 'product_cat', $product->get_id() ) && ! is_product() ){
$price .= '<span style="float:right"><i class="fas fa-shipping-fast"></i></span> ';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'append_icon_after_product_price', 10, 2 );
function append_icon_after_product_price( $price, $product ) {
if( is_product_category( 'fast-shipping' ) ){
$price .= '<span style="float:right"><i class="fas fa-shipping-fast"></i></span> ';
}
return $price;
}
add_filter('woocommerce_get_price_html','prepend_append_icon_to_price',10,2);
函数prepend_将_图标_追加到_价格($price,$product){
如果(具有术语('fast shipping'、'product_cat'、$product->get_id())和&!is_product()){
$price.='';
}
返回$price;
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作
2) 在特定的Woocommerce产品类别归档页面上:
add_filter( 'woocommerce_get_price_html', 'prepend_append_icon_to_price', 10, 2 );
function prepend_append_icon_to_price( $price, $product ) {
if( has_term( 'fast-shipping', 'product_cat', $product->get_id() ) && ! is_product() ){
$price .= '<span style="float:right"><i class="fas fa-shipping-fast"></i></span> ';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'append_icon_after_product_price', 10, 2 );
function append_icon_after_product_price( $price, $product ) {
if( is_product_category( 'fast-shipping' ) ){
$price .= '<span style="float:right"><i class="fas fa-shipping-fast"></i></span> ';
}
return $price;
}
add_filter('woocommerce_get_price_html','append_icon_在产品价格之后,'10,2');
函数在产品价格($price,$product)之后附加图标{
如果(是产品类别(“快速装运”)){
$price.='';
}
返回$price;
}
代码进入活动子主题(或活动主题)的function.php文件。已测试并正常工作。您好,欢迎使用堆栈溢出。请编辑问题以包含您的相关代码,因为没有它,任何人都不可能复制您的情况。另外,请不要一次问多个问题。谢谢。好的,我会的,谢谢!这成功了!!至少对于简单的产品来说是这样。有没有一种方法可以使它也适用于可变产品?万分感谢@Matt我已经重新测试了代码,它也可以在我的测试服务器上的可变产品上运行…好的。。我将再看一看我的其他代码,看看是否有什么东西阻止它在可变产品上工作。再次感谢你的帮助!!!如何编写自定义css以将图标定位到最右侧?现在,图标显示在价格之后。解决了这个问题,必须将css设置为打开。价格设置为内联块,效果非常好!