Advertisement
businessdad

Currency Switcher - Dynamic multi-currency price filter

May 3rd, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.51 KB | None | 0 0
  1. /**
  2.  * Aelia Currency Switcher - Filtering products with dynamic multi-currency pricing.
  3.  * The following code improves the accuracy of the price filtering widget, by matching the minimum
  4.  * and maximum price against the product prices in the active currency.
  5.  *
  6.  * IMPORTANT
  7.  * For an accurate filtering, the product prices have to be calculated on the fly, as they depend
  8.  * on factors such as the exchange rates at the moment of filtering. This means that the filtering
  9.  * logic can be significantly heavier and, consequently, slower. We recommend that you test this
  10.  * code on a staging copy of your site, and optimise it for your own specific setup, before using
  11.  * it on a production site.
  12.  *
  13.  * HOW TO USE THIS CODE
  14.  * Simply add the code to the bottom of your theme's functions.php file, and it
  15.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  16.  *
  17.  * GPL DISCLAIMER
  18.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  19.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  20.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  21.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  22.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  23.  *
  24.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  25.  */
  26. add_filter('loop_shop_post_in', function($product_ids) {
  27.   $max_price = $_GET['max_price'];
  28.   $min_price = $_GET['min_price'];
  29.  
  30.   // Eventually, alter the parameters to exclude products that would be hidden
  31.   // regardless of the price, e.g. hidden products, out of stock products, etc.
  32.   $posts = get_posts(array(
  33.     'post_type' => 'product'
  34.   ));
  35.  
  36.   foreach($posts as $entry) {
  37.     // Skip any product that might already be in the list
  38.     if(in_array($entry->ID, $product_ids)) {
  39.       continue;
  40.     }
  41.    
  42.     $product = wc_get_product($entry);
  43.     // Get the price on the fly. This will return the price in the active currency,
  44.     // converted automatically if needed
  45.     $product_price = $product->get_price();
  46.     if($product_price >= $min_price && product_price <= $max_price) {
  47.       $product_ids[] = $entry->ID;
  48.     }
  49.   }
  50.  
  51.   return $product_ids;
  52. }, 10, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement