I am using the following function to order woocommerce products on product category pages:
add_filter('woocommerce_get_catalog_ordering_args', function($args){
if ( !is_product_category() ) return $args;
set_query_var('meta_query',[
'relation' => 'AND',
'stock_status_clause' => [ 'key' => '_stock_status','type' => 'CHAR' ],
'price_clause' => [ 'key' => '_price', 'type' => 'NUMERIC'],
'custom_cat_rank_clause' => [ 'key' => '_custom_cat_rank', 'type' => 'NUMERIC']
]);
//https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
$args["orderby"] = ['stock_status_clause' => 'ASC', 'price_clause' => 'ASC', 'title' => 'ASC', 'custom_cat_rank_clause' => 'ASC' ];
return $args;
},100);
On certain categories and for certain products, I want to change _custom_cat_rank value for the product. For example – if _custom_cat_rank is set as 2 for a product in wp_postmeta table, I want it to be changed to 1 on certain category pages conditionally without having to change it in the database so that the product ranking on those category pages is modified. This will enable product ranking modification conditionally on a per product basis. How to do this?