PHP code example of commerceguys / zone

1. Go to this page and download the library: Download commerceguys/zone 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/ */

    

commerceguys / zone example snippets


use CommerceGuys\Addressing\Address;
use CommerceGuys\Zone\Model\Zone;
use CommerceGuys\Zone\Model\ZoneMemberCountry;

$zone = new Zone();
$zone->setId('german_vat');
$zone->setName('German VAT');
$zone->setScope('tax');

// Create the German VAT zone (Germany and 4 Austrian postal codes).
$germanyZoneMember = new ZoneMemberCountry();
$germanyZoneMember->setCountryCode('DE');
$zone->addMember($germanyZoneMember);

$austriaZoneMember = new ZoneMemberCountry();
$austriaZoneMember->setCountryCode('AT');
$austriaZoneMember->setIncludedPostalCodes('6691, 6991:6993');
$zone->addMember($austriaZoneMember);

// Check if the provided austrian address matches the German VAT zone.
$austrianAddress = new Address();
$austrianAddress = $austrianAddress
    ->withCountryCode('AT')
    ->withPostalCode('6692');
echo $zone->match($austrianAddress); // true

use CommerceGuys\Addressing\Address;
use CommerceGuys\Zone\Matcher\ZoneMatcher;
use CommerceGuys\Zone\Repository\ZoneRepository;

// Initialize the default repository which loads zones from json files stored in
// resources/zone. A different repository might load them from the database, etc.
$repository = new ZoneRepository('resources/zone');
$matcher = new ZoneMatcher($repository);

$austrianAddress = new Address();
$austrianAddress = $austrianAddress
    ->withCountryCode('AT')
    ->withPostalCode('6692');

// Get all matching zones.
$zones = $matcher->matchAll($austrianAddress);
// Get all matching zones for the 'tax' scope.
$zones = $matcher->matchAll($austrianAddress, 'tax');

// Get the best matching zone.
$zone = $matcher->match($austrianAddress);
// Get the best matching zone for the 'shipping' scope.
$zone = $matcher->match($austrianAddress, 'shipping');