How can I display prices on my website? Print

  • 0

Firstly you need to download the Blesta API, and upload them to your Blesta installation: https://github.com/phillipsdata/blesta_sdk

You then need to make an API user account on Blesta: Settings > System > API Access > Add API User

Now you need to make a php file in your website folder e.g: api.php and paste in the following:

<?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 add more and more of these per product just rename them:
$price1 = getPackagePrice($api, 1, "month", 1); & $price1_amount = formatCurrency($api, $price1->price, $price1->currency, $selected_currency, $company_id);

If you put the above in a external file you will need to include it on your site's php page where you want to display the prices:

<?php
        require_once "/home/username/public_html/api.php";
?>

And then all you need to do to display the price is: <?php echo $price1_amount; ?>

Errors

Error:  and A€, etc

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

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

Fix: $price3 = getPackagePrice($api, 19, "onetime", 0);

Thanks to CodyTyson and Josh (Serverbin.net) for the code, together they helped get this code working for us.


Was this answer helpful?

« Back