Lari Salminen
Lari Salminen

Reputation: 31

Woocommerce - Show featured products first in category

Just set up my first shop (affiliate site) using Woocommerce. On the category page, can I somehow show featured products first in the list of products displayed?

For example, if I have chosen to check the star for a featured product, is there any way I can show it first of all?

I know there are some shortcodes and plugins that do something like it, but I want it to show by default in the products category page.

Haven't been able to try anything, since I don't know what to do.

Upvotes: 3

Views: 2360

Answers (2)

Aipok
Aipok

Reputation: 11

I have also been looking for this for a long time and have solved it this way so far:

  1. First, I installed the Featured Products First for WooCommerce plugin.

Note: This plugin is free. Basically, it has certain limitations. Only a few Featured Products are displayed first in the categories and on the store page.

  1. I got around this limit by simply adding code to your function.php in your Child theme.

Code:

//add new sorting option
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['recommended'] = 'Featured';
    return $sortby;
}

//set default sorting for new option
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
  $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    if ( 'recommended' == $orderby_value ) {
        $args['orderby'] = 'date';
        $args['order'] = 'DESC';
        $args['meta_key'] = '';
    }
    return $args;
}

//adjust order to allow for featured posts
add_filter('posts_orderby', 'show_featured_products_orderby',10,2);
function show_featured_products_orderby($order_by, $query){
  global  $wpdb ;
  if( (!is_admin()) ){
    $orderby_value = ( isset( $_GET['orderby'] ) ? wc_clean( (string) $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ) );
    $orderby_value_array = explode( '-', $orderby_value );
    $orderby = esc_attr( $orderby_value_array[0] );
    $order = ( !empty($orderby_value_array[1]) ? $orderby_value_array[1] : 'DESC' );
    $feture_product_id = wc_get_featured_product_ids();
    //only apply to recommended sorting option
    if ( $orderby == "recommended" && is_array( $feture_product_id ) && !empty($feture_product_id) ) {
      if ( empty($order_by) ) {
        $order_by = "FIELD(" . $wpdb->posts . ".ID,'" . implode( "','", $feture_product_id ) . "') DESC ";
      } else {
        $order_by = "FIELD(" . $wpdb->posts . ".ID,'" . implode( "','", $feture_product_id ) . "') DESC, " . $order_by;
      }
    }  
  }
  return $order_by;
}

  1. Then go to the Customize section - Woocommerce - Product catalog - Default product sorting and set to Featured
  2. Save

Now your Featured Products should appear first (at the top) on the store page and in all categories. By latest date added. Other products that are not Featured are displayed by the most recent Addition date under Featured products. Example: You add the products Product 1: on 7/10/2023, Product 2: on 8/15/2023, Product 3: on 8/20/2023 and Product 4: on 9/10/2023 You mark Product 4 and Product 2 as Featured and leave the others unmarked.

Subsequently, the products will be sorted as follows: First will be Product 4, then Product 2, because they are marked as Featured and were sorted by date of addition from the most recent. Then other products will be displayed. Product 3 and finally Product 1, also by date of addition from the most recent.

It's not the best method, but it's free and works for me. Good luck

Upvotes: 1

Arsin Torabi
Arsin Torabi

Reputation: 31

go to product edit page -> advanced tab

then :

set -1 for order setting

all product have this value move on top the list in category list.

Upvotes: 3

Related Questions