Based on Custom product price suffix for selected product categories in WooCommerce answer code, I have used the following to change the price suffix in a specific product category to ‘per kg’. However, there are two products in this category I need to exclude from this. Could anyone tell me whether this is possible please.
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('cheese');
if( has_term( $product_categories, 'product_cat', $product->get_id() ) )
$price .= ' ' . __('per kg');
return $price;
}
Many thanks
Use the following (where you will define the product ids to be excluded):
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('cheese');
// HERE define your products Ids to be excluded
$excluded_ids = array(37, 57);
if( has_term( $product_categories, 'product_cat', $product->get_id() )
&& ! in_array( $product->get_id(), $excluded_ids ) ) {
$price .= ' ' . __('per kg');
}
return $price;
}
Code goes in function.php file of your active child theme (active theme). It should works.