Custom Sort By
-
I’m trying to add a custom sort by option to the drop down when viewing products. I’ve added the following code, but when the option is selected, I’m routed to https://new.tmvavra.com/search/?orderby=test and I get a No Products Found message. I suspect I’m not using the correct meta-key value but can’t seem to get it to work. What am I missing?
function custom_product_sort( $sort_args ) {
// Get the selected order by value
$orderby_value = isset( $_GET[‘orderby’] ) ? woocommerce_clean( $_GET[‘orderby’] ) : apply_filters( ‘woocommerce_default_catalog_orderby’, get_option( ‘woocommerce_default_catalog_orderby’ ) );switch ($orderby_value) {
case ‘alphabetical’ :
$sort_args[‘order’] = ‘ASC’;
$sort_args[‘meta_key’] = ”;
$sort_args[‘orderby’] = ‘title’;
break;
case ‘test’ :
$sort_args[‘post_type’] = ‘product’;
$sort_args[‘order’] = ‘ASC’;
$sort_args[‘meta_key’] = ‘bathrooms’;
$sort_args[‘orderby’] = ‘meta_value_num’;
break;
}return $sort_args;
}
add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_product_sort’ );function add_custom_product_sort_options( $sortby ) {
$sortby[‘alphabetical’] = ‘Sort by name’;
$sortby[‘test’] = __( ‘Sort by Test’, ‘textdomain’ );
return $sortby;
}
add_filter( ‘woocommerce_default_catalog_orderby_options’, ‘add_custom_product_sort_options’ );
add_filter( ‘woocommerce_catalog_orderby’, ‘add_custom_product_sort_options’ );The page I need help with: [log in to see the link]
- The topic ‘Custom Sort By’ is closed to new replies.