Reputation: 29
In woocommerce I am using "Add text before product price if it's higher than a specific amount in Woocommerce" answer code that adds text before all prices.
How can I make this code work only for a specific product category?
Any help is appreciated.
Upvotes: 2
Views: 2685
Reputation: 253784
The following code will work for a specific product category. The code handles parent product categories and product variations, using a custom conditional function for product categories:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
// HERE set your product category in the array
$product_category = array('clothing');
// Only on frontend and excluding min/max prices for variable products
if( is_admin() || $product->is_type('variable') )
return $price_html;
// Get the variable product ID for product variations (as variations dont handle product categories)
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Only for a specific product category
if( ! has_product_categories( $product_id, $product_category ) )
return $price_html;
// Get product price
$price = (float) $product->get_price(); // Regular price
if( $price > 15 )
$price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
return $price_html;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You can also use has_term()
conditional function (not handling parent product categories):
add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
// HERE set your product category in the array
$product_category = array('clothing');
// Only on frontend and excluding min/max prices for variable products
if( is_admin() || $product->is_type('variable') )
return $price_html;
// Get the variable product ID for product variations (as variations dont handle product categories)
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Only for a specific product category
if( ! has_product_categories( $product_id, $product_category ) )
return $price_html;
// Get product price
$price = (float) $product->get_price(); // Regular price
if( $price > 15 )
$price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
return $price_html;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Related thread: Add text before product price if it's higher than a specific amount in Woocommerce
Upvotes: 1
Reputation: 337
This should work: Added has_term for category to the existing answer to that question.
if ( has_term( 'putcategorynamehere', `'product_cat'` ) ) {
add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
// Only on frontend and excluding min/max prices on variable products
if( is_admin() || $product->is_type('variable') )
return $price_html;
// Get product price
$price = (float) $product->get_price(); // Regular price
if( $price > 15 )
$price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
return $price_html;
}
}
Upvotes: 0