Jump to content

Add currency selector on custom page


IamAQ

Recommended Posts

Hello again, 

I am trying to add a currency selector on one of my custom page where I have display the product details.
I want to allow my vistor to change the default currency from this custom page and view the product pricing accordingly.

I have added this code in my custom.tpl 

    <form method="post" action="" >
                                <input type="hidden" name="token" value="{$LANG.go}">
                                <select name="currency" onchange="submit()" class="form-control">
                                    <option value="">Change Currency (A$ AUD)</option>
                                    {foreach from=$currencies item=listcurr}
                                        <option value="{$listcurr.id}"{if $listcurr.id == $currency.id} selected{/if}>{$listcurr.prefix} {$listcurr.code}</option>
                                    {/foreach}
                                </select>
                            </form>


But it is not working, everyytime I select the currency from dropdown, it refresh the page. but nothing changes,
please guide what I am doing wrong.

Thanks in advance
Aqsa,

 

Link to comment
Share on other sites

how are you getting the product pricing on the custom page? are you using a hook, passing a currency value in a data feed or some other way?

I will have previously posted solutions to this  - the first two should be covered in the post below....

the problem is not necessarily the dropdown itself, it's how you're passing the currency to the page and then how you're getting the price based on that passed value.

it can be done, I just need to know more about which way you're trying to do it.

Link to comment
Share on other sites

5 hours ago, brian! said:

the problem is not necessarily the dropdown itself, it's how you're passing the currency to the page and then how you're getting the price based on that passed value.

 

I am using hook to fetch product details. But while debuging I see the products array has a muliple copies of same product with different currency, (means same product has different index in array for different currencies.) So It display same product table multiple time for multiple currency.

like this

12.JPG

Link to comment
Share on other sites

I am also concerned about the product's sequence, even if I manage to hide the repeated products, there sequence is not right. as you may see in the image below,
 

5454.thumb.JPG.48147582b93d68e6b5ee16c321b25294.JPG

 

But on the deault cart, it has the accuracte sequence , as you may see below.


2121.JPG.a00094415ae675acc96b879a121771a1.JPG

 

Edited by IamAQ
Link to comment
Share on other sites

Hi Aqsa,

16 hours ago, IamAQ said:

I am using hook to fetch product details. But while debugging I see the products array has a multiple copies of same product with different currency, (means same product has different index in array for different currencies.) So It display same product table multiple time for multiple currency.

ideally, you should be using a currency value in your hook query - otherwise it will, as you have found, either get all results for a given pid, or the first result (depending on the query used).

->where('tblpricing.currency', $currencyid)

your currency dropdown should be passing a value for each currency, e.g page.php?currency=1 and then the hook gets that passed currency value and uses it in the query - that way you know the query should only be searching for selected product prices in the chosen currency.

dmb62Ok.gif

you can't really tell from the image, but each time i'm clicking on a currency link, the page is reloading and the hook runs again, using the currency being passed to it, to pull the product names and format the monthly pricing.

16 hours ago, IamAQ said:

I am also concerned about the product's sequence, even if I manage to hide the repeated products, there sequence is not right. as you may see in the image below,

if you aren't specifying a sort order in the query (in Capsule, that's orderBy), then it will just return the results in the order that it finds them, e.g the order they were added to WHMCS)...

->orderBy('tblproducts.order')

i'm assuming that you are querying tblproducts with a join to tblpricing ? i'd probably need to see the hook you're using to see where any specific faults are.

Link to comment
Share on other sites

2 hours ago, brian! said:

i'm assuming that you are querying tblproducts with a join to tblpricing ? i'd probably need to see the hook you're using to see where any specific faults are.

Yes, I am querying tblproducts with a join to tblpricing. Here is the hook I am using .

 

<?php
 
use Illuminate\Database\Capsule\Manager as Capsule;
 
function waka_products($vars) {
 
   $products = Capsule::table('tblproducts')
                       ->join('tblpricing''tblproducts.id''=''tblpricing.relid')
                       ->select('tblproducts.*','tblpricing.*')
                       ->where('tblproducts.gid','10')
                       ->where('tblpricing.currency''1')
                       ->where('tblpricing.type''product')
                       ->orderBy('tblproducts.order')
                       ->get();
 
   $encodedata = json_encode($products);
   $decodedata = json_decode($encodedatatrue);
 
   return array("myproducts" => $decodedata);
}
add_hook("ClientAreaPage"1"waka_products");
?>
Link to comment
Share on other sites

19 hours ago, IamAQ said:

In above hook I have updated the query after your recommendation, Now product sequence and repeated product issue is resolved.  but currencies are still not updating on the page.

aahh - you didn't get my little clue in the reply where I emboldened the word "gets"... perhaps I should have made it red too! 😎

<?php

# Currency Selector On Custom Page Hook
# Written by brian!

use WHMCS\Database\Capsule;

function waka_products($vars)
{
	$currencyid = $_GET['currency'];
	$validcurrencies = array_column($vars['currencies'],'id');
	if (!in_array($currencyid,$validcurrencies)) { $currencyid = 1;	}
	$products = Capsule::table('tblproducts')
		->join('tblpricing','tblproducts.id','=','tblpricing.relid')
		->where('tblpricing.type', 'product')
		->where('tblproducts.hidden','0')
		->where('tblproducts.gid','10')
		->where('tblpricing.currency', $currencyid)
		->orderBy('tblproducts.order')						
		->get();
	$results = json_decode(json_encode($products), true);
	return array("myproducts" => $results);
}
add_hook("ClientAreaPage", 1, "waka_products");

so the four differences from your hook are:

  1. i've added the code that checks for the value of the currency being passed in the URL.
  2. the query will now use that passed currency value to get the pricing in the selected currency - if currency value is not a valid currency value, then it defaults to 1 (which I assume is your default currency - change if it's not).... though if an invalid value is passed, the dropdown might show the wrong selected currency - but that shouldn't really matter as they should only be using currency selections from the dropdown choices.
  3. i've removed the select because if you're keeping everything from the result, you don't need to specify a select - ideally, you should be selecting only the info you need though rather than everything.
  4. i'm checking that the returned products aren't set to be hidden.

personally, i'd also make three additional changes - not included as i'm sure you just want to get it working first before you clean it up.

  1. I would add an if statement in the hook so that it only runs on the custom page - as written, it's going to run on *every* client area page.
  2. if you're only interested in a specific billing cycle, I would check that it's values aren't -1, e.g it has a valid price set for it.
  3. I would format the price in the correct currency in the hook - though I assume you're going to do it in the template using {$currency.prefix} and {$currency.suffix} which is fine.

and if it helps, I can give you the modified Smarty dropdown too...

{if !$loggedin && $currencies}
	<select name="currency" class="form-control select-inline" onchange="if (this.value) window.location.href=this.value">
		<option value="">{$LANG.choosecurrency}</option>
			{foreach $currencies as $currchoice}
				<option value="{$smarty.server.PHP_SELF}?currency={$currchoice.id}" {if $currchoice.id|intval eq $smarty.session.currency}selected{/if}>{$currchoice.prefix} {$currchoice.code}</option>
			{/foreach}
	</select>
{/if}

Uq2hfDb.gif

Edited by brian!
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated