我有一家 WooCommerce 在线商店。我想仅在列出所有产品的产品列表页面(如商店页面)上显示自定义价格后缀。
我有以下代码:
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 );
}
但是使用此代码,后缀将显示在产品列表页面和单个产品上。有人可以帮助我吗?
以下内容将在所有产品列表中显示额外的自定义价格后缀(单个产品除外):
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 文件中。已测试并有效。
正如评论中提到的,您可以使用
is_shop()
功能来检查您是否在商店页面上,如下所示:
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
if ( is_shop() ) $price .= ' ' . __('Suffix');
return $price;
}