• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Please help with displaying Recommended products in Woocommerce

Aipok

New member
Nov 8, 2020
17
4
3
Hello Brothers
Please help. I am trying to add Featured Sort in the store catalog as the first featured in Woocommerce
To do this I used the Hooks below and put in the function.php file in the child theme.
Everything works. Recommended products are displayed first in the store and categories.
The problem is that the recommended products are displayed from the oldest date to the newest. Then the other products are displayed from newest to oldest and that's fine. Finally, products that are not in stock.
I'm trying to get featured products to show up first in the store, from the most recently added to the oldest added.
Can someone please help me?
Here is the hook I used.
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] : 'ASC' );
    $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;
}
 

Forum statistics

Threads
79,456
Messages
1,142,841
Members
248,447
Latest member
Mr_Fekkak
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock