Order by several meta_key
-
I’ve got two different meta_key I have to order by on the same ordering option chosen by the visitor.
If visitor chooses “Vivino rating” I need to order by “vivino_rating ASC” and then “_price_with_tax DESC”.
If visitor chooses “Price” I need to order by “_price_with_tax ASC” and then “vivino_rating ASC”.
How can I set two “orderby”, “meta_key” and “order” on the woocommerce_get_catalog_ordering_args filter?
Here’s my current code:
/* Products ordering */ add_filter('woocommerce_catalog_orderby', 'custom_products_order_options'); add_filter('woocommerce_default_catalog_orderby_options', 'custom_products_order_options'); function custom_products_order_options($sortby) { //var_dump($sortby); //We can unset unset($sortby['popularity']); unset($sortby['rating']); unset($sortby['date']); unset($sortby['price']); unset($sortby['price-desc']); //Add option - Popularity - Our ranking $sortby['_our_rating_desc']=__('Sort by ranking: high to low', 'vinha'); //Add option - Price with tax $sortby['_custom_price_with_tax']=__('Sort by price with tax: low to high', 'vinha'); return $sortby; } add_filter('woocommerce_get_catalog_ordering_args', 'custom_products_order_order'); function custom_products_order_order($args) { //var_dump($args); $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); //each of this options needs to be a secondary ordering factor of the other one. How? if ($orderby_value=='_our_rating_desc') { global $wpdb; $args['orderby']='meta_value_num '.$wpdb->posts.'.ID'; $args['meta_key']='_our_rating'; $args['order']='DESC'; //I need to add _custom_price_with_tax ASC as secondary order } if ($orderby_value=='_custom_price_with_tax') { global $wpdb; $args['orderby']='meta_value_num '.$wpdb->posts.'.ID'; $args['meta_key']='_custom_price_with_tax'; $args['order']='ASC'; //I need to add _our_rating DESC as secondary order } return $args; }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Order by several meta_key’ is closed to new replies.