Skip to Content
Account
Enter the email address on your account.
Forgot Your Username?
Enter your password.
Forgot Your Password?

Or continue with

Developer

GeoZoneField Field Type

3 Min Read
On This Page

GeoZoneField extends Joomla's ListField to render a <select> populated with every enabled geo-zone from #__j2commerce_geozones. Geo-zones group geographic regions (countries, zones) for use in tax rules, shipping rate tables, and other location-based pricing logic. The stored value is the integer primary key j2commerce_geozone_id.

Class Reference

PropertyValue
ClassGeoZoneField
NamespaceJ2Commerce\Component\J2commerce\Administrator\Field
Fileadministrator/components/com_j2commerce/src/Field/GeoZoneField.php
ExtendsJoomla\CMS\Form\Field\ListField
Field type tokenGeoZone
Since6.0.7

Database Query

SELECT j2commerce_geozone_id AS value, geozone_name AS text
FROM #__j2commerce_geozones
WHERE enabled = 1
ORDER BY geozone_name ASC

The stored value is j2commerce_geozone_id (integer).

XML Usage

Basic Usage

<field
    name="geozone_id"
    type="GeoZone"
    label="COM_MYPLUGIN_FIELD_GEOZONE_LABEL"
    description="COM_MYPLUGIN_FIELD_GEOZONE_DESC"
/>

With Placeholder Option

<field
    name="geozone_id"
    type="GeoZone"
    label="COM_MYPLUGIN_FIELD_GEOZONE_LABEL"
    required="true">
    <option value="">COM_MYPLUGIN_SELECT_GEOZONE</option>
</field>

Supported XML Attributes

All standard ListField attributes are supported:

AttributeTypeDescription
namestringForm field name
labelstringLanguage key for the field label
descriptionstringLanguage key for the tooltip
defaultstring/integerDefault selected geo-zone ID
requiredbooleanWhether a value is required
multiplebooleanAllow multi-select
classstringAdditional CSS classes on the <select> element

Usage in Plugin Forms

<?xml version="1.0" encoding="utf-8"?>
<form addfieldprefix="J2Commerce\Component\J2commerce\Administrator\Field">

    <fieldset name="basic">

        <field
            name="geozone_id"
            type="GeoZone"
            label="COM_MYPLUGIN_FIELD_GEOZONE_LABEL"
            description="COM_MYPLUGIN_FIELD_GEOZONE_DESC"
            default="0">
            <option value="0">COM_MYPLUGIN_ALL_GEOZONES</option>
        </field>

    </fieldset>

</form>

In a Plugin Manifest <config> Block

<config>
    <fields name="params" addfieldprefix="J2Commerce\Component\J2commerce\Administrator\Field">
        <fieldset name="basic">
            <field
                name="geozone_id"
                type="GeoZone"
                label="COM_MYPLUGIN_FIELD_GEOZONE_LABEL"
            />
        </fieldset>
    </fields>
</config>

Source Code

// File: administrator/components/com_j2commerce/src/Field/GeoZoneField.php

declare(strict_types=1);

namespace J2Commerce\Component\J2commerce\Administrator\Field;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\ListField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseInterface;

class GeoZoneField extends ListField
{
    protected $type = 'GeoZone';

    public function getOptions(): array
    {
        $options = parent::getOptions();

        $db    = Factory::getContainer()->get(DatabaseInterface::class);
        $query = $db->getQuery(true)
            ->select([
                $db->quoteName('j2commerce_geozone_id', 'value'),
                $db->quoteName('geozone_name', 'text')
            ])
            ->from($db->quoteName('#__j2commerce_geozones'))
            ->where($db->quoteName('enabled') . ' = 1')
            ->order($db->quoteName('geozone_name') . ' ASC');

        $db->setQuery($query);

        foreach ($db->loadObjectList() as $geozone) {
            $options[] = HTMLHelper::_('select.option', $geozone->value, $geozone->text);
        }

        return $options;
    }
}

Notes

  • Only geo-zones with enabled = 1 appear. Disabled geo-zones are excluded.
  • Geo-zones are managed under J2Commerce -> Configuration -> Geo Zones.
  • The stored value j2commerce_geozone_id can be used to look up #__j2commerce_geozonerules to retrieve the specific countries and zones within the geo-zone.

Still stuck? Our team can help.

Back to Top