WooCommerce: Programmatically Mark Products As Featured

For store owners managing hundreds of products, setting featured items manually is inefficient. A dynamic approach can save admin time by automatically marking products as featured based on specific conditions, such as category, price, or custom logic.

Normally, you’d mark WooCommerce products as featured by logging into wp-admin and clicking the “star” icon in the Products table. But what if you could automate this without clicking anything?

Thankfully, WooCommerce provides a filter hook that allows you to override the featured status on the fly. This means you can apply rules programmatically while keeping your admin time at zero.

Here’s how you can achieve this with a simple PHP snippet!

I had no intention of clicking on the star for each of these 6 products by hand, so I came up with the snippet below!

PHP Snippet #1: Conditionally Mark WooCommerce Product IDs as Featured

This function uses a WooCommerce filter to automatically mark certain products as featured without changing their data in the database. Here’s a breakdown:

  1. add_filter: This tells WooCommerce to apply the custom function whenever it checks whether a product is featured.
  2. bbloomer_mark_products_as_featured: This is the custom function that checks the featured status.
  3. Product Check: Inside the function, we check if the product’s ID is one of the specified IDs (123, 456, 789). If it is, the product will be marked as featured.
  4. Return Featured Status: If the product ID matches, it returns true, marking the product as featured. Otherwise, it keeps the original status ($featured).

In simple terms, this function automatically marks certain products as featured based on their ID without manual actions.

/**
 * @snippet       Set WooCommerce Featured Products Programmatically
 * @tutorial      https://businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_product_get_featured', 'bbloomer_mark_products_as_featured', 9999, 2 );

function bbloomer_mark_products_as_featured( $featured, $product ) {
    if ( in_array( $product->get_id(), [ 123, 456, 789 ] ) ) {
        return true;
    }    
    return $featured;
}

PHP Snippet #2: Conditionally Mark a Whole WooCommerce Product Category as Featured

  1. add_filter: This adds a filter that hooks into WooCommerce’s process of determining whether a product is featured.
  2. bbloomer_mark_products_as_featured: This is the custom function that will decide whether a product is featured based on category.
  3. has_term: This checks if the product belongs to the ‘yoga’ category.
  4. Return true: If the product is in the ‘yoga’ category, the function marks it as featured.
  5. Return original status: If the product isn’t in the ‘yoga’ category, the original featured status is kept.

This code will automatically mark all products in the ‘yoga’ category as featured, without modifying the database.

/**
 * @snippet       Set WooCommerce Featured Products Programmatically
 * @tutorial      https://businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_product_get_featured', 'bbloomer_mark_products_as_featured', 9999, 2 );

function bbloomer_mark_products_as_featured( $featured, $product ) {
    if ( has_term( 'yoga', 'product_cat', $product->get_id() ) ) {
        return true;
    }    
    return $featured;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *