仅在所有 WooCommerce 产品循环中显示价格后缀

Show Price Suffix only on all WooCommerce Product loops

我在 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 );
}

但是使用此代码,后缀显示在产品列表页面和单个产品上。谁能帮帮我?

如评论中所述,您可以使用 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;
}

以下将在所有产品列表中显示额外的自定义价格后缀(单个产品除外)

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 文件。已测试并有效。