问题
I have the following function which adds a custom sort by discount amount:
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'] = 'Sort by discount';
return $sortby;
}
I'd like to update this to sort by
- The discount percentage (descending)
- Shows all products (including variable products)
I've found this answer which gets the percentage but I don't know how to use it in the above.
来源:https://stackoverflow.com/questions/61574132/woocommerce-sort-all-products-by-sale-percentage-including-variable-products