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.
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;
}