I am using the following code in my function.php of my wordpress theme. And it sorts and display only the single products, not sorting/listing the variable products which are on discount/sale.
add_filter( 'woocommerce_get_catalog_ordering_args', 'wcs_get_catalog_ordering_args' );
function wcs_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'on_sale' == $orderby_value ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
$args['meta_key'] = '_sale_price';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'wcs_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'wcs_catalog_orderby' );
function wcs_catalog_orderby( $sortby ) {
$sortby['on_sale'] = 'Хямдралын хувиар';
return $sortby;
}
Therefore, how can I sort/display both simple & variable products which are on discount/sale
2
Answers
Check this one. It’s Work for me in my scenario.
FYR :- https://www.cloudways.com/blog/woocommerce-product-sort-and-display/
I know it is a bit old question, however, the solution I found might be helpful for someone.
After checking and trying different options, here is what worked for me. Results are amazing!