我在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 );
}
但是使用此代码,后缀显示在“产品列表”页面和单个产品上。谁能帮我吗?
答案 0 :(得分:2)
以下内容将在所有产品列表(单个产品除外) 上显示附加的自定义价格后缀:
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文件中。经过测试,可以正常工作。
答案 1 :(得分:1)
如评论中所述,您可以使用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;
}