Jump to content
IamAQ

Add currency selector on custom page

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,

 

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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");
?>

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
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!

Share this post


Link to post
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.


  • Similar Content

    • By IamAQ
      Hello, 

      I am trying to add client's products and services list to the custom.php page, 
      I have use the local API and added this code in custom.php file,
       $command = 'GetClientsProducts';  $postData = array(      'clientid' => '1',      'stats' => true,  );  $adminUsername = 'admin'; // Optional for WHMCS 7.2 and later    $results = localAPI($command, $postData, $adminUsername);  print_r($results);  It displays the results in Json format with print_r, but  I don't understand how I can use this in .tpl file to display the client's products on the page.
      Please Help!
    • By sawfly
      Hi to all. I had a task to migrate old custom page (from 5th version) to 7th version. I have a problem with a primary navbar
      I am getting navbar with 6 items in it
      count(Menu::primaryNavbar()->getChildren()); // 6 But on the site in the primary navbar, there are 7 items. I tried to check out in hook the code above and got 7; There is no 'affiliates' item in the navbar. Maybe someone knows how to get full primary navbar as in all pages on the site
    • By larryk
      I am trying to create a custom WHMCS page without any sidebars. I only want it to show the top bar where clients can choose language, etc.
      I have this code currently:
      <?php define("CLIENTAREA",true); //define("FORCESSL",true); // Uncomment to force the page to use https:// require("init.php"); $ca = new WHMCS_ClientArea(); $ca->setPageTitle("Support Center"); $ca->addToBreadCrumb('index.php',$whmcs->get_lang('globalsystemname')); $ca->addToBreadCrumb('support.php','HostAble Support'); $ca->addToBreadCrumb('support-center.php','Support Center'); $ca->initPage(); # Check login status if ($ca->isLoggedIn()) { # User is logged in - put any code you like here # Here's an example to get the currently logged in clients first name $result = mysql_query("SELECT firstname FROM tblclients WHERE id=".$ca->getUserID()); $data = mysql_fetch_array($result); $clientname = $data[0]; $ca->assign('clientname', $clientname); } else { # User is not logged in } # Define the template filename to be used without the .tpl extension $ca->setTemplate('supportcenter'); $ca->output(); ?> But obviously it isnt working, it just shows the page but with the sidebars and navigation bar too.
      Thank you if you can help me :)
    • By bigideaguy
      Hey guys!
       
      I was unable to find a good way of updating bitcoin currency rates so I decided to create my own module to do this for me and that resultet in Anveto Currency Rates.
       

       
      This addon will update additional currency rates when the daily cron runs or when Anveto cron runs (see downloads here for anveto cron https://anveto.com/members/downloads/2/WHMCS) in order to update currencies multiple times daily. Currently it uses the bitpay (https://bitpay.com/api/rates) to get currency rates but you could set this to any source as long as it follows the same json format.
       
      The module costs $10 and can be purchased here https://anveto.com/members/cart.php?a=add&pid=21
       
      It is currently pending community app store review.
       
      The source code is not encrypted as I want users to be able to modify this however they would like as long as they purchase the module.
       
      I hope some of you find it useful!
    • By chateaux
      I would like to sell some of my hosting in different currencies as we have a number of markets that we deal with.
       
      Essentially I want to set and forget the prices:
       
      A: Shared hosting in the US - $9.95 / m (USD is the base currency)
      B: Shared hosting in the EU - €9.95 / m
       
      I don't want the system to convert anything from A -> B or from B -> A, the prices are fixed regardless of the exchange rate fluctuation. The reason being that if prices are linked to A, and someone has paid B, their rate could go up and down each month. Now some of our hosting is in countries where the exchange rate fluctuations are pretty big which is causing people to think twice about a USD pegged hosting.
       
      I note when adding a product, I can add the fixed price in a given currency however this seems to cause problems:
       
      1. When placing an order in currency B, customer ends up with a $0.00 in the basket and no product selected
      2. When a customer logs into their account and searches for products, all products in currency B are not listed
       
      Is there a resolution for this short of setting up a WHMCS install for each regions (not a workable option).
       
      thanks
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • 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