1

In a WooCommerce site, we have products that have a custom taxonomy that are assign to a region.

When the user visits the site, we activate the browser geolocation, and we assign the user a region in a cookie.

Now we would like to show, in the shop page and in the product_cat taxonmies, in first the products that are assign to the same region of the user and then all other regions.

How could we implement this ?

So far, I taught of using pre_get_posts, and using something like :

add_action('pre_get_posts', 'order_by_region', 999999);
function order_by_region($q){
 if(!is_admin() && $q->is_main_query() && (is_shop() || is_tax('product_cat'))){
  $q->set('orderby', 'meta_value');
  $q->set('order', 'DESC');
  $q->set('value', $_COOKIE['nameofusercookie']);
  $q->set('meta_key', 'regions');

 }

 return $q;
}

But this didn't work. I just found that in there it will filter the posts instead of ordering by regions.

I found the 'the_posts' hook. That contains all the products after quering but not yet looped. I think it might be better place to sort all the products with the cookie. But how do i compare the cookie and the regions taxonomy in each products ?

add_filter('the_posts', 'sort_query_by_region', 10, 2);
function sort_query_by_region($posts, $q){
 $cookie = $_COOKIE['selected_region'];

 if(isset($cookie) && !empty($cookie)){
  if(!is_admin() && $q->is_main_query() && (is_shop() || is_tax('product_cat'))){
   $posts = sortByRegion($posts);
  }
 }
 return $posts;
}

function sortByRegion($posts){
 usort($posts, "regionOrder");
 return $posts;
}

function regionOrder($a, $b) {
   // get the cookie
   $cookie = $_COOKIE['slected_region'];

  // Get the term region of each compared product
  $regionA = get_the_terms($a->ID, 'regions')[0]->slug;
  $regionB = get_the_terms($b->ID, 'regions')[0]->slug;

  if($regionA != $cookie || $regionB != $cookie){
   return ($a, $b);
  } else {
   return $a < $b;
  }
}

I must that either doesn't work.

Anyone everdone such a re-ordering and knows I to help me?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.