Custom product price suffix for selected product categories in Woocommerce

与世无争的帅哥 提交于 2019-12-02 00:18:01

Try the following code that uses has_term() conditional function for defined product categories:

add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
    // HERE define your product categories (can be IDs, slugs or names)
    $product_categories = array('t-shirts','hoodies');

    if( has_term( $product_categories, 'product_cat', $product->get_id() ) )
        $price .= ' ' . __('per M/T');

    return $price;
}

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


To exclude defined product categories you will replace:

if( has_term( $product_categories, 'product_cat', $product->get_id() ) )

simply by:

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