仅在所有 WooCommerce 产品循环中显示价格后缀 [英] Show Price Suffix only on all WooCommerce Product loops
本文介绍了仅在所有 WooCommerce 产品循环中显示价格后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一家 WooCommerce 在线商店.我只想在列出所有产品的产品列表页面(如商店页面)上显示自定义价格后缀.
I have an online shop with WooCommerce. I want to show a custom price Suffix only on the Product List Page (like Shop Page), where all products are listed.
我有以下代码:
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ){
$price = $price . ' Suffix ';
return apply_filters( 'woocommerce_get_price', $price );
}
但使用此代码,后缀显示在产品列表页面和单个产品上.有人可以帮我吗?
But with this code, the suffix is display in the Product list Page and on single Products. Can anyone help me?
推荐答案
以下内容将在所有产品列表中显示额外的自定义价格后缀(单个产品除外):
The following will show an additional custom price suffix on all product listings (except on single products):
add_filter( 'woocommerce_get_price_suffix', 'additional_price_suffix', 999, 4 );
function additional_price_suffix( $html, $product, $price, $qty ){
global $woocommerce_loop;
// Not on single products
if ( ( is_product() && isset($woocommerce_loop['name']) && ! empty($woocommerce_loop['name']) ) || ! is_product() ) {
$html .= ' ' . __('Suffix');
}
return $html;
}
或者你也可以使用:
add_filter( 'woocommerce_get_price_html', 'additional_price_suffix', 100, 2 );
function additional_price_suffix( $price, $product ){
global $woocommerce_loop;
// Not on single products
if ( ( is_product() && isset($woocommerce_loop['name']) && ! empty($woocommerce_loop['name']) ) || ! is_product() ) {
$price .= ' ' . __('Suffix');
}
return $price;
}
代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
这篇关于仅在所有 WooCommerce 产品循环中显示价格后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文