PHP code example of alexedimensionz / royal-mail-price-calculator

1. Go to this page and download the library: Download alexedimensionz/royal-mail-price-calculator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexedimensionz / royal-mail-price-calculator example snippets




RoyalMailPriceCalculator\Calculator;
use \RoyalMailPriceCalculator\Package;
use \RoyalMailPriceCalculator\Services\GuaranteedByOnePmService;
use \RoyalMailPriceCalculator\Services\FirstClassService;

$calculator = new Calculator();

$package = new Package();
$package->setDimensions(15, 15, 0.4);
$package->setWeight(90);

$calculator->setServices(array(
							new FirstClassService(), 
							new GuaranteedByOnePmService()));

foreach ($calculator->calculatePrice($package) as $calculated)
{
    echo $calculated['service']->getName() . "\n";
    foreach ($calculated['prices'] as $price) {
        echo "  →  £{$price['price']} (Compensation: £{$price['compensation']})\n";
    }
    echo "\n";
}



RoyalMailPriceCalculator\Calculator;
use \RoyalMailPriceCalculator\Package;
use \RoyalMailPriceCalculator\Services\InternationalTracked;
use \RoyalMailPriceCalculator\Services\InternationalEconomy;

$calculator = new Calculator();

$package = new Package();
$package->setDimensions(15, 15, 0.4);
$package->setWeight(90);

// This part is mandatory for international shipments
$target_iso = 'US';
$calculator->setCountryCode($target_iso);
//


$calculator->setServices(array(
                         	new InternationalTracked(), 
                         	new InternationalEconomy()));


// Note: there is no compensation value for international
foreach ($calculator->calculatePrice($package) as $calculated)
{
    echo $calculated['service']->getName() . "\n";
    foreach ($calculated['prices'] as $price) {
        echo "  →  £{$price['price']}\n";
    }
    echo "\n";
}



RoyalMailPriceCalculator\Calculator;