skip to Main Content

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


  1. Check this one. It’s Work for me in my scenario.

    function cw_add_postmeta_ordering_args( $args_sort_cw ) {
      $cw_orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) :
            apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
      switch( $cw_orderby_value ) {
        case 'points_awarded':
          $args_sort_cw['orderby'] = 'meta_value_num';
          $args_sort_cw['order'] = 'desc';
          $args_sort_cw['meta_key'] = 'points';
          break;
           case 'location':
                $args_sort_cw['orderby'] = 'meta_value';
                $args_sort_cw['order'] = 'asc';
                $args_sort_cw['meta_key'] = 'location';
                break;
      }
      return $args_sort_cw;
    }
    add_filter( 'woocommerce_get_catalog_ordering_args', 'cw_add_postmeta_ordering_args' );
    function cw_add_new_postmeta_orderby( $sortby ) {
       $sortby['location'] = __( 'Sort By Location', 'woocommerce' );
       $sortby['points_awarded'] = __( 'Sort By Sale Point', 'woocommerce' );
       return $sortby;
    }
    add_filter( 'woocommerce_default_catalog_orderby_options', 'cw_add_new_postmeta_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'cw_add_new_postmeta_orderby' );
    

    FYR :- https://www.cloudways.com/blog/woocommerce-product-sort-and-display/

    Login or Signup to reply.
  2. 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!

    add_action( 'woocommerce_product_query', 'my_shop_product_query' );
    function my_shop_product_query( $q ){
        if($q->query['orderby'] == 'on_sale'){ // this is very important condition to set otherwise it will affect your whole main query
            $sale_product_IDs = wc_get_product_ids_on_sale();
            $q->set( 'post__in', $sale_product_IDs );
            $q->set( 'orderby', 'post__in' );
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search