I have used the hook "woocommerce_get_catalog_ordering_args" and I can edit the sort by, but can't figure out how to search params and sort by product names before descriptions. Any ideas?
add_action( 'woocommerce_get_catalog_ordering_args', 'sort_by_quantity', 900);
function sort_by_quantity( $args ) {
$args['orderby'] = 'meta_value';
$args['order'] = 'ASC';
$args['meta_key'] = '_stock_status';
return $args;
};
Edit:
Just to clarify my example of what I need...
If I was to have a product with the name Purple Pants, and someone was to search pants. They should see "Purple Pants" before any products that don't have "pants" in their product name. (ex. description)
In the example below you can see I have searched 006 but the second product doesn't have 006 in the name and the third product does. I want to make sure the hierarchy is product name over description.
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_product_sorting' );
function custom_product_sorting( $args ) {
$search = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ));
// Show products in stock first
if( 'in-stock' === $search ) {
$args['meta_key'] = '_stock_status';
$args['orderby'] = array( 'meta_value' => 'ASC' );
}
if('relevance' === $search) {
$args['meta_key'] = ['','_stock_status'];
$args['orderby'] = ['relevance' => "desc", 'meta_value' => 'ASC'];
}
return $args;
}