Skip to content

Shipping Zone Model ​

DB Table Name{wp_db_prefix}_fct_shipping_zones
SchemaCheck Schema
Source Filefluent-cart/app/Models/ShippingZone.php
Name SpaceFluentCart\App\Models
ClassFluentCart\App\Models\ShippingZone

Traits ​

  • FluentCart\App\Models\Concerns\CanSearch - Provides search(), groupSearch(), whereLike(), whereBeginsWith(), whereEndsWith() scopes

Appended Attributes ​

The following computed attributes are automatically appended to the model's array/JSON output:

  • formatted_region - Human-readable region name

Attributes ​

AttributeData TypeComment
idIntegerPrimary Key
shipping_class_idIntegerScopes the zone to one shipping class; NULL means it applies to class-less items
nameStringShipping zone name
regionStringA country code, 'all' (Whole World) or 'selection' (multiple countries, listed in meta)
metaArrayFor 'selection' zones: countries and selection_type (included / excluded)
orderIntegerDisplay order
created_atDate TimeCreation timestamp
updated_atDate TimeLast update timestamp

Usage ​

Please check Model Basic for Common methods.

Accessing Attributes ​

php
$shippingZone = FluentCart\App\Models\ShippingZone::find(1);

$shippingZone->id; // returns id
$shippingZone->name; // returns zone name
$shippingZone->region; // returns region code
$shippingZone->order; // returns display order
$shippingZone->formatted_region; // returns formatted region name (appended attribute)

Relations ​

This model has the following relationships that you can use

shippingClass ​

Access the shipping class this zone is scoped to, if any.

  • return FluentCart\App\Models\ShippingClass Model or null

Example: ​

php
$class = $shippingZone->shippingClass;

methods ​

Access all shipping methods in this zone. Results are ordered by id descending.

  • return FluentCart\App\Models\ShippingMethod Model Collection

Example: ​

php
// Accessing Methods
$methods = $shippingZone->methods;

// For Filtering by methods relationship
$shippingZones = FluentCart\App\Models\ShippingZone::whereHas('methods', function($query) {
    $query->where('is_enabled', 1);
})->get();

Methods ​

Along with Global Model methods, this model has few helper methods.

appliesToCountry($country) ​

Whether this zone covers the given country. Returns true for region = 'all'. For region = 'selection' it checks meta.countries honouring meta.selection_type (included or excluded). Otherwise it compares region to the country code.

  • Parameters
    • $country - string (country code)
  • Returns bool

Usage ​

php
if ($shippingZone->appliesToCountry('DE')) {
    // zone covers Germany
}

getFormattedRegionAttribute() ​

Get formatted region name (accessor). Returns 'Whole World' if region is 'all', otherwise resolves the country code to its full name via AddressHelper::getCountryNameByCode().

  • Parameters
    • none
  • Returns string

Usage ​

php
$formattedRegion = $shippingZone->formatted_region; // e.g., "United States" or "Whole World"

Usage Examples ​

Get Shipping Zones ​

php
$shippingZone = FluentCart\App\Models\ShippingZone::find(1);
echo "Zone Name: " . $shippingZone->name;
echo "Region: " . $shippingZone->region;
echo "Formatted Region: " . $shippingZone->formatted_region;

Create Shipping Zone ​

php
$shippingZone = FluentCart\App\Models\ShippingZone::create([
    'name' => 'United States',
    'region' => 'US',
    'order' => 1
]);

Get Shipping Zones with Methods ​

php
$shippingZones = FluentCart\App\Models\ShippingZone::with('methods')->get();

foreach ($shippingZones as $zone) {
    echo "Zone: " . $zone->name;
    foreach ($zone->methods as $method) {
        echo "  - Method: " . $method->title;
    }
}

Get Zones by Region ​

php
$usZones = FluentCart\App\Models\ShippingZone::where('region', 'US')->get();
$allWorldZones = FluentCart\App\Models\ShippingZone::where('region', 'all')->get();

Get Zones Ordered by Display Order ​

php
$orderedZones = FluentCart\App\Models\ShippingZone::orderBy('order', 'asc')->get();

Get Zones with Enabled Methods ​

php
$zonesWithEnabledMethods = FluentCart\App\Models\ShippingZone::whereHas('methods', function($query) {
    $query->where('is_enabled', 1);
})->get();

Update Shipping Zone ​

php
$shippingZone = FluentCart\App\Models\ShippingZone::find(1);
$shippingZone->update([
    'name' => 'United States & Canada',
    'order' => 2
]);

Get Zone by Name ​

php
$zone = FluentCart\App\Models\ShippingZone::where('name', 'United States')->first();

Get Zones with Method Count ​

php
$zonesWithCounts = FluentCart\App\Models\ShippingZone::withCount('methods')->get();

foreach ($zonesWithCounts as $zone) {
    echo "Zone: " . $zone->name . " (" . $zone->methods_count . " methods)";
}

Delete Shipping Zone ​

php
$shippingZone = FluentCart\App\Models\ShippingZone::find(1);
$shippingZone->delete();

FluentCart developer documentation