Jump to content

Display Prices On Your Website.


Michael

Recommended Posts

Thanks to Cody, Tyson and Josh (Serverbin.net) for the code, together they helped get this code working for us the community and here is a little tutorial on how to get this working.
 
Ok firstly you need to download the brand new API which Cody has developed to make it easier for developers from:
 
https://github.com/phillipsdata/blesta_sdk (Click on download zip)
 
Put the files into the root of the Blesta install.
 
 
Then you need to make a API user and get the hash for the connection to your Blesta install.
 
 
After you've done that you can do the following, you need to put this on the php file you wish the prices to be displayed on:
 
If you aren't using a SSL on your installation change https://blestainstallationurl.com/api to http://blestainstallationurl.com/api
 

<?php
require_once "/home/username/public_html/blesta_api.php";

$user = "API USERNAME";
$key = "API KEY";
$url = "https://blestainstallationurl.com/api/";
$api = new BlestaApi($url, $user, $key);

$company_id = 1;

// Set acceptable currencies
$valid_currencies = array("USD", "GBP", "EUR");
// Set default currency
$selected_currency = "USD";

// Set another currency if given
if (isset($_GET['currency']) && in_array(strtoupper($_GET['currency']), $valid_currencies))
    $selected_currency = strtoupper($_GET['currency']);

$price1 = getPackagePrice($api, 1, "month", 1);


$price1_amount = formatCurrency($api, $price1->price, $price1->currency, $selected_currency, $company_id);


function getPackagePrice($api, $package_id, $period, $term) {

    $package = $api->get("packages", "get", array('package_id' => $package_id))->response();

    $package_price = null;
    foreach ($package->pricing as $price) {
        // Get monthly term
        if ($price->period == $period && $price->term == $term) {
            return $price;
        }
    }
    return false;
}


function formatCurrency($api, $amount, $from_currency, $to_currency, $company_id) {
    // Do the currency conversion
    $amount = $api->get("currencies", "convert", array('amount' => $amount, 'from_currency' => $from_currency, 'to_currency' => $to_currency, 'company_id' => $company_id))->response();

    // Format the currency
    return $api->get("currencies", "toCurrency", array('value' => $amount, 'currency' => $to_currency, 'company_id' => $company_id))->response();
}
?>

 
You can alter the code for more than one product like the example for mine:
 

<?php
require_once "/home/cwadminb/public_html/blesta_api.php";

$user = "CubicWebs_API";
$key = "dbf8a2e7bd87bea1c79d868dba5a56f3";
$url = "https://billing.cubicwebs.com/api/";
$api = new BlestaApi($url, $user, $key);

$company_id = 1;

// Set acceptable currencies
$valid_currencies = array("USD", "GBP", "EUR");
// Set default currency
$selected_currency = "USD";

// Set another currency if given
if (isset($_GET['currency']) && in_array(strtoupper($_GET['currency']), $valid_currencies))
    $selected_currency = strtoupper($_GET['currency']);

$price1 = getPackagePrice($api, 1, "month", 1);
$price2 = getPackagePrice($api, 2, "month", 1);
$price3 = getPackagePrice($api, 3, "month", 1);
$price4 = getPackagePrice($api, 4, "month", 1);


$price1_amount = formatCurrency($api, $price1->price, $price1->currency, $selected_currency, $company_id);
$price2_amount = formatCurrency($api, $price2->price, $price2->currency, $selected_currency, $company_id);
$price3_amount = formatCurrency($api, $price3->price, $price3->currency, $selected_currency, $company_id);
$price4_amount = formatCurrency($api, $price4->price, $price4->currency, $selected_currency, $company_id);


function getPackagePrice($api, $package_id, $period, $term) {

    $package = $api->get("packages", "get", array('package_id' => $package_id))->response();

    $package_price = null;
    foreach ($package->pricing as $price) {
        // Get monthly term
        if ($price->period == $period && $price->term == $term) {
            return $price;
        }
    }
    return false;
}


function formatCurrency($api, $amount, $from_currency, $to_currency, $company_id) {
    // Do the currency conversion
    $amount = $api->get("currencies", "convert", array('amount' => $amount, 'from_currency' => $from_currency, 'to_currency' => $to_currency, 'company_id' => $company_id))->response();

    // Format the currency
    return $api->get("currencies", "toCurrency", array('value' => $amount, 'currency' => $to_currency, 'company_id' => $company_id))->response();
}
?>

 
Where you wish the price to be put the following php code:
 

<?php echo $price1_amount; ?>

 
Change the number to the product price you wish to display.
 
Again thanks to the coders for helping the community.
 

If you used this code when I posted it please update it as it has a fix from Tyson to ensure the GBP prices don't just change the USD symbol.

Edited by CubicWebs
Link to comment
Share on other sites

Another tip guys, if you get A£ etc and A€
 
you need to put this in the head section of your website pages:

        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">


If you get the following error when doing onetime prices: 

number_format() expects parameter 1 to be double, string given

 

change them to:

 

$price3 = getPackagePrice($api, 19, "onetime", 0);
$price4 = getPackagePrice($api, 20, "onetime", 0);
Link to comment
Share on other sites

I need to figure out how to do this with page caching. It affects my page load times pretty drastically on any page that I'm accessing the api from.

 

It would be nice if I had a script that would calculate these for me ever so often via cron tasks.. maybe once a week or something.

Link to comment
Share on other sites

I need to figure out how to do this with page caching. It affects my page load times pretty drastically on any page that I'm accessing the api from.

 

It would be nice if I had a script that would calculate these for me ever so often via cron tasks.. maybe once a week or something.

really? are you using the require_once?

Link to comment
Share on other sites

Yeah. I mean it isn't terrible, but it's noticeable.

 

Check out this page -> http://dev.memoryx2.com/

 

Notice how fast it loads then click on the web hosting tab. It is definately noticeable.

 

 

Maybe because I put it in the same file so it reloads every time.. Just thought about this.

 

Oh I see haha Its only started happening on mine.

 

http://licensecart.com/site/

 

http://cubicwebs.xxx/shared-hosting

 

http://gtmetrix.com/reports/cubicwebs.xxx/1r5eQRsx

Link to comment
Share on other sites

Yeah. I mean it isn't terrible, but it's noticeable.

 

Check out this page -> http://dev.memoryx2.com/

 

Notice how fast it loads then click on the web hosting tab. It is definately noticeable.

 

 

Maybe because I put it in the same file so it reloads every time.. Just thought about this.

I don't get that now? 

 

http://tools.pingdom.com/fpt/#!/ddanDA/http://cubicwebs.xxx/ - Netherlands - EU

http://tools.pingdom.com/fpt/#!/eyaMuJ/http://cubicwebs.xxx/ - Dallas - TX

http://tools.pingdom.com/fpt/#!/ciRFwt/http://cubicwebs.xxx/ - New York - NY

Link to comment
Share on other sites

  • 6 months later...

I will give this a try as I did have live pricing from WHMCS when we where using it.

I've also got a ticket in with support on finding out what I need to query to report back number of rows in a product id (web hosting, domains, vps's).

 

E.g. How many web hosting packages are we hosting.

 

So I can display it on our site.

Link to comment
Share on other sites

  • 2 weeks later...

Been using your script to do pricing on our site.

How can I remove GBP from appearing on the end?

 

 

Also it seems to take a few seconds longer to load the page.

Is there a quicker way of pulling pricing? Anyone got MySQL queries?

 

Do you have GBP on the end of your prices in the cart? If you do you can remove it from the currency screen in the admin area, and it should remove it from the site.

Link to comment
Share on other sites

Do you have GBP on the end of your prices in the cart? If you do you can remove it from the currency screen in the admin area, and it should remove it from the site.

 

Perfect yes that's worked.

 

As for caching, If anyone could help with that that would be fab. Not quite sure how to go about it.

Link to comment
Share on other sites

I created my own caching system in the end using cron jobs.

 

On another note I've finaly got around to doing our domains page.

$COUK = getPackagePrice($api, 20, "year", 1);

However this shows as £0.00 unless I set the billing to 1 Year instead of 2 Years.

 

How can I get it to show the price when it's set to minimum of 2 Years?

 

Cheers :)

Link to comment
Share on other sites

I created my own caching system in the end using cron jobs.

On another note I've finaly got around to doing our domains page.

$COUK = getPackagePrice($api, 20, "year", 1);
However this shows as £0.00 unless I set the billing to 1 Year instead of 2 Years.

How can I get it to show the price when it's set to minimum of 2 Years?

Cheers :)

Change the 1 to a 2 and it should work we do for SSL prices.

Link to comment
Share on other sites

  • 2 weeks later...

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.

Loading...
×
×
  • Create New...