• 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)
  • Plugin Author Oscar Gare

    (@oscargare)

    Hi, thank you so much for your contribution.
    I’ll fix this bug in next release.

    Regards.

    Thread Starter dmasin

    (@dmasin)

    Minor update, I did not realise that local prices are not labelled by country codes in the database, but by the region codes (I am using country codes as region names in my case, so this is why it worked for me). Fix is simple, attached below. David

    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'];
                    $regionkey=$key;
                }
            }
    
            if ( isset($regionkey) ) {
                $local_price='_'.strtolower($regionkey).'_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;
    }
    Plugin Author Oscar Gare

    (@oscargare)

    Hi @dmasin,
    In version 1.6 I have rewritten the core of the plugin and “sort by price” works fine with a little piece of code.
    Can you give me some feedback about?

    Thank you.

    Thread Starter dmasin

    (@dmasin)

    Hi Oscar,
    I tested it now and it seems to work directly with your plugin (without my additional peace of code which I presented earlier in this post).
    Well, it did not work straight away on my website, probably because my theme is using some outdated woocommerce customisation, but I tried to switch to most recent Storefront on my local install and it works.
    Thank you for taking care
    David

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘sort-by-price dos not work’ is closed to new replies.