Jump to content

Search by price in local currency (with ServiceCurrencyConversion module)


hellomoto
 Share

Recommended Posts

I installed ServiceCurrencyConversion and it's great. 

I was wondering if any of you by chance had any experience with using this as I would expect it would often be used, to search by price... in your currency. 

Right now I have products that each have fields price_currency and price_amount. So each has a native currency. I am able to show the price in each currency we currently support on our listings index by way of this function:

function currencyConvert($page) {
	//echo $page->id . '<br/>';
	$cc = wire('modules')->get('ServiceCurrencyConversion');
	$current = $page->price_currency->name;
	$amt = $page->price_amount;
	$currencies = wire("pages")->find('parent=1135,include=all');
	//echo count($currencies) . '<br/>';
	$converted = [];

	foreach($currencies as $currency) {
		//echo $currency->name;
		//if($currency = $cc) continue;
		//$converted["$currency->name"] = $cc->convert($current, $currency->name, $page->price_amount);
		//if($current != $currency->name) {
			//echo " Set: $current Now: $currency->name Amt: $page->price_amount ";
			$converted[$currency->name] = $cc->convert(strToUpper($current), strToUpper($currency->name), $page->price_amount);
			$amount = $currency->name == 'eur' ? number_format($converted[$currency->name], 0, ',', ' ') : number_format($converted[$currency->name]);
			$converted[$currency->name] = wire("pages")->find("parent=1135, name=$currency->name, include=all")->first()->title . $amount;
		//}
	}
	//print_r($converted);
	return $converted;
	//echo '<br/><br/>';
}

So in my listings index I have:

			foreach(wire("pages")->find('parent=1135, include=all') as $currency) {
				if($currency->name != $item->price_currency->name) $price .= ' / ' . currencyConvert($item)[$currency->name];
			}

That outputs the price in every currency that isn't the default for the listing (which is already output). 

So I already have a search in place for listings that searches by the price_amount. 

I want to incorporate the different currencies in my search, while not excluding results based on their set currency. The way I have this set up currently works, but incorrectly, as it doesn't restrict listings based on their currency conversion. Now that I can convert them I want to fix the search feature to accommodate this. 

So how can I do this? I would think to maybe store a separate price_amount per currency, with a default set (to base other values from), but this does seem a bit tricky... Right now only 3 currencies are supported but if it grows to be a large amount it might become unwieldy. If that's the right way to do it though, so I will. Or, just leave the fields as-is, and go by the chosen currency in the search, returning all results that match the amount in that currency. This is what I would like to do because it seems simpler. But then I worry about performance... This means every search would have to convert the price for potentially almost every listing, and that's before even returning a set of results!(?) Like, you would have to search, with a currency and max. and/or min. amount set for price... then that search would have to first process the converted price for every listing that fits all the other search criteria, right (potentially all), and then return the values that match the price range in the chosen currency. That sounds like too much to me. 

Also obviously outputting the price in every currency we have would get too lengthy so I want to be able to target a single one efficiently.

You see my dilemma? Anyone more knowledgeable have any advice, please? Much appreciated.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...