1
votes

by default custom order + name is set in my woocommerce product sorting. we have products with certain order and we want when someone enters the category to sort items first by the order we have them set and then from less to more price. i was trying the below code but that isn't working and i don't understand further.

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_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 ( 'new_default_list' == $orderby_value ) {
        $args['orderby'] = 'menu_order price';
        $args['order'] = 'asc';
        $args['meta_key'] = '';
    }
    return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['new_default_list'] = 'Menu Order + Price';
    return $sortby;
}
1

1 Answers

0
votes

This is not an answer, but another approach trying to achieve the same of the original question. This code orders the items by price correctly, but items without price are not displayed. How can I make sure all products are displayed?

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_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 ( 'new_default_list' == $orderby_value ) {
        $args['orderby'] = 'menu_order meta_value_num';
        $args['order'] = 'desc';
        $args['meta_key'] = '_price';
    }
    return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['new_default_list'] = 'Custom Order, then Price: high to low';
    return $sortby;
}