Show Price Suffix only on all WooCommerce Product loops

强颜欢笑 提交于 2021-02-04 07:50:56

问题


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.

I have the following code:

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?


回答1:


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;
}

Or you can also use:

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;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.




回答2:


As mentioned in the comments you can use the is_shop() function to check if you are on the shop page like this:

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
    if ( is_shop() ) $price .= ' ' . __('Suffix');
    return $price;
}


来源:https://stackoverflow.com/questions/62922063/show-price-suffix-only-on-all-woocommerce-product-loops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!