Trying query woocommerce product order by menu order and stock status

Question

I am trying to make the query order by menu order DSC, and put all out of stock at the very end by combining the below 2 codes into one.

This one works ordering by menu order

add_action( 'woocommerce_product_query', function( $query ) {
    if ( is_admin() ) return;
    $query->set( 'order', 'DSC' );
    $query->set( 'orderby', 'menu_order' );
    },
    PHP_INT_MAX - 10
);

And This one work for stock status

add_action( 'woocommerce_product_query', function( $query ) {
    if ( is_admin() ) return;
    $query->set( 'meta_key', '_stock_status' );
    $query->set( 'order', 'ASC' );
    $query->set( 'orderby', 'meta_value' );
    },
    PHP_INT_MAX - 10
);

When I do something like this. But have no luck as well.

add_action( 'woocommerce_product_query', function( $query ) {
    if ( is_admin() ) return;
    $query->set( 'meta_key', '_stock_status' );
    $query->set( 'orderby', array(
        'meta_value' => 'ASC', 
        'menu_order' => 'DSC') 
               );
    },
    PHP_INT_MAX - 10
);

0
Blue Li 1 week 2022-09-17T07:47:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse