/**
* Aelia Currency Switcher - Filtering products with dynamic multi-currency pricing.
* The following code improves the accuracy of the price filtering widget, by matching the minimum
* and maximum price against the product prices in the active currency.
*
* IMPORTANT
* For an accurate filtering, the product prices have to be calculated on the fly, as they depend
* on factors such as the exchange rates at the moment of filtering. This means that the filtering
* logic can be significantly heavier and, consequently, slower. We recommend that you test this
* code on a staging copy of your site, and optimise it for your own specific setup, before using
* it on a production site.
*
* HOW TO USE THIS CODE
* Simply add the code to the bottom of your theme's functions.php file, and it
* will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
*
* GPL DISCLAIMER
* Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
* Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
* without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
* is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
*
* Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
*/
add_filter('loop_shop_post_in', function($product_ids) {
$max_price = $_GET['max_price'];
$min_price = $_GET['min_price'];
// Eventually, alter the parameters to exclude products that would be hidden
// regardless of the price, e.g. hidden products, out of stock products, etc.
$posts = get_posts(array(
'post_type' => 'product'
));
foreach($posts as $entry) {
// Skip any product that might already be in the list
if(in_array($entry->ID, $product_ids)) {
continue;
}
$product = wc_get_product($entry);
// Get the price on the fly. This will return the price in the active currency,
// converted automatically if needed
$product_price = $product->get_price();
if($product_price >= $min_price && product_price <= $max_price) {
$product_ids[] = $entry->ID;
}
}
return $product_ids;
}, 10, 1);
Comments