sort-by-price dos not work
-
I recently switched my website https://rockngemshow.com to “WooCommerce Product Price Based on Countries” and I am more than happy so far, I just noticed that “sort by price” does not work correctly if manual price_method is used – products are still sorted by the main _price. So I wrote a filter (to go to functions.php) which corrects this. Attached below, in the case someone finds it useful, or maybe even for the plugin developer who may want to clean-up this function and include it in the plugin itself.
/*-------------------------------------------------------------------------------------------------- Fix ordering by price using "WooCommerce Product Price Based on Countries" plugin --------------------------------------------------------------------------------------------------*/ add_filter('woocommerce_get_catalog_ordering_args', 'custom_products_order_order', 999); function custom_products_order_order($args) { global $wpdb; if ($args['meta_key']=='_price' && $args['orderby']=='meta_value_num') { add_filter('posts_clauses', 'custom_order_by_price', 999); } return $args; } function custom_order_by_price($args) { global $wpdb; $customer_country=WC()->customer->get_country(); $regions = get_option( 'wc_price_based_country_regions', array() ); foreach($regions as $key=>$region) { $countries_in_region=$region['countries']; if( in_array( $customer_country, $countries_in_region ) ) $exchange_rate=$region['exchange_rate']; } if ( isset($exchange_rate) ) { $local_price='_'.strtolower($customer_country).'_price'; $local_price_method=$local_price.'_method'; if ( isset( $_SERVER['QUERY_STRING'] ) ) { parse_str( $_SERVER['QUERY_STRING'], $params ); } $order = ! empty( $params['product_order'] ) ? $params['product_order'] : 'asc'; $order = strtoupper( $order ); $args['join'] .= " LEFT JOIN $wpdb->postmeta AS wc_price ON ($wpdb->posts.ID = wc_price.post_id AND wc_price.meta_key = '_price') LEFT JOIN $wpdb->postmeta AS wc_price_local ON ($wpdb->posts.ID = wc_price_local.post_id AND wc_price_local.meta_key = '$local_price') LEFT JOIN $wpdb->postmeta AS wc_price_local_method ON ($wpdb->posts.ID = wc_price_local_method.post_id AND wc_price_local_method.meta_key = '$local_price_method') "; $args['orderby'] = " IF(wc_price_local_method.meta_value LIKE 'manual', wc_price_local.meta_value+0, wc_price.meta_value * $exchange_rate ) $order"; } remove_filter('posts_clauses', 'custom_order_by_price'); return $args; }
https://www.ads-software.com/plugins/woocommerce-product-price-based-on-countries/
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘sort-by-price dos not work’ is closed to new replies.