WooCommerce sort all products by sale percentage including variable products

时间秒杀一切 提交于 2021-02-11 14:02:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!