I use this code to sort by discount in Woocommerce.
But sort only the simple products.

How filter also the variable togheter with simple ?

/**
* Add "Sort by discount" to sorting options. Defaults to biggest to smallest 
discount.
*/
add_filter( 'woocommerce_get_catalog_ordering_args', 
'mycode_woocommerce_add_salediscount_to_catalog_ordering_args' );
function mycode_woocommerce_add_salediscount_to_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 ( 'discount' == $orderby_value ) {
    $args['orderby']    = 'meta_value_num';
    $args['order']      = 'DESC';
    $args['meta_key']   = '_sale_price';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 
'mycode_woocommerce_add_salediscount_to_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 
'mycode_woocommerce_add_salediscount_to_catalog_orderby' );
function mycode_woocommerce_add_salediscount_to_catalog_orderby( $sortby ) {
$sortby['discount']     = 'Sort by discount';
return $sortby;
}

thanks

Read more here: Sort by discount WooCommerce


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: