Agile Store Locator provides support for programmatically retrieving stores using PHP. To obtain the stores, you can utilize the following method:
$clauses = [];
$limit = 100;
$offset = 0;
\AgileStoreLocator\Model\Store::get_stores($clauses, $limit, $offset);
You can pass various parameters in the $clauses array to filter the store values. The available parameters include:
title: Filter by store title.description: Filter by store description.street: Filter by store street address.city: Filter by store city.state: Filter by store state.postal_code: Filter by store postal code.phone: Filter by store phone number.fax: Filter by store fax number.email: Filter by store email.is_disabled: Filter by disabled store (since version 5.1.4).
$clauses = ['city' => 'NY'];
$limit = 100;
$offset = 0;
\AgileStoreLocator\Model\Store::get_stores($clauses, $limit, $offset);
By customizing these parameters in the $clauses array, you can retrieve the desired store information programmatically.
Create Cards without PHP #
If you prefer to avoid using PHP, Agile Store Locator also provides store cards that can be displayed using the [ASL_CARDS] shortcode. You can pass parameters to customize the displayed stores in a similar manner, you can follow the store cards documentation.
Create a New Store Programatically #
// Create a new store programatically
if(method_exists("\AgileStoreLocator\Admin\Store", 'add_new_store')) {
$store_data = [
"title" => "Bid-Bon Development",
"description" => "bid bid",
"street" => "274 Kragga Kamma Road, Lorraine",
"city" => "Port Elizabeth",
"state" => "Eastern Cape",
"postal_code" => "23452",
//"lat" => "-33.96524",
//"lng" => "25.50242",
"phone" => "041 888 3534",
];
// Todo, change it
$have_coordinates = false;
// Add Business Hours (JSON accepted)
$store_data['open_hours'] = '{"mon":["09:30 AM - 06:30 PM"],"tue":["09:30 AM - 06:30 PM"],"wed":["09:30 AM - 06:30 PM"],"thu":["09:30 AM - 06:30 PM"],"fri":["09:30 AM - 06:30 PM"],"sat":"0","sun":"0"}';
// I don't have coordinates :( Let's fetch it
if(!$have_coordinates) {
$google_server_key = \AgileStoreLocator\Helper::get_configs('server_key');
// 1- Get the Coordinates for the provided zipcode
$coordinates = \AgileStoreLocator\Helper::getCoordinates($store_data['street'], $store_data['city'], $store_data['state'], $store_data['postal_code'], $store_data['country'], $google_server_key);
// Found coordinates?
if(!empty($coordinates)) {
$store_data['lat'] = $coordinates['lat'];
$store_data['lng'] = $coordinates['lng'];
}
}
// Add it to request['data']
$_REQUEST['data'] = $store_data;
// Custom Fields
$_REQUEST['asl-custom'] = ['field_name_1' => 'xyz', 'field_name_2' => 'abc'];
// Create an instance
$store_inst = new \AgileStoreLocator\Admin\Store();
// Must be returned as object, else it will die
$store_inst->as_object = true;
// Create Store
$response = $store_inst->add_new_store();
echo '<pre>';
print_r($response);
echo '/<pre>';
die;
}
Access All Categories #
if(method_exists("\AgileStoreLocator\Model\Category", 'get_categories')) {
$lang = ''; // Keep empty for default
// Get the Stores Categories
$categories = \AgileStoreLocator\Model\Category::get_categories($lang);
}
To access them in parent and child hierarchy.
if(method_exists("\AgileStoreLocator\Model\Category", 'get_categories')) {
$lang = ''; // Keep empty for default
// Get the Stores Categories
$categories = \AgileStoreLocator\Model\Category::get_app_categories($lang);
}
Get All the Store Titles #
The code below can be used to get the titles of all the stores, or any other attributes.
// Check if the 'get_stores' method exists in the Store model
if (method_exists("\AgileStoreLocator\Model\Store", 'get_stores')) {
// Retrieve all stores using the get_stores method
$stores = \AgileStoreLocator\Model\Store::get_stores();
// Extract the 'title' column from the stores array and remove duplicates
$store_titles = array_unique(array_column($stores, 'title'));
// Initialize the dropdown HTML with a select element for store titles
$store_dropdown = '<select id="asl-stores-title" name="store-titles">';
// Loop through each unique store title and create an option element
foreach ($store_titles as $title) {
$store_dropdown .= '<option value="' . esc_attr($title) . '">' . esc_html($title) . '</option>';
}
// Close the select element
$store_dropdown .= '</select>';
// Output the complete dropdown
echo $store_dropdown . '<br>';
}
else {
// Fallback message if the get_stores method does not exist
echo 'Store data could not be loaded.';
}
Programmatically Find the Nearest Store #
Agile Store Locator provides a built-in helper method that lets you fetch the closest store(s) to a given latitude and longitude directly from PHP. This is useful for custom integrations such as checkout logic, location-based routing, third-party APIs, or any workflow where you need to determine the nearest store programmatically.
Parameters #
| Parameter | Description |
|---|---|
| $origLat | Origin latitude. |
| $origLon | Origin longitude. |
| $dist | Maximum search radius in miles. |
| $clauses | (Optional) Associative array for additional filters, such as categories, custom fields, or store metadata. Example: ['categories' => 12]. |
| $limit | Number of stores to return. Default is 1 (the closest store). If set higher, the method returns an array of store objects ordered by distance. |
Example Usage #
$lat = 40.7128;
$lng = -74.0060;
$radius = 25; // miles
if ( class_exists('\AgileStoreLocator\Helper') ) {
$filters = ['categories' => 5];
$closest = \AgileStoreLocator\Helper::get_closest_store($lat, $lng, $radius, $filters);
if ($closest) {
echo 'Nearest store: ' . $closest->title . ' (' . $closest->distance . ' miles away)';
}
}
Getting Multiple Nearby Stores #
If you need more than one result, simply increase the $limit:
if ( class_exists('\AgileStoreLocator\Helper') ) {
$stores = \AgileStoreLocator\Helper::get_closest_store($lat, $lng, $radius, [], 5);
foreach ($stores as $store) {
echo $store->title . ' - ' . $store->distance . " miles\n";
}
}
The returned list will be sorted automatically from nearest to farthest.
