Store Locator WordPress Hooks/Filters

Store Locator for WordPress offers multiple hooks or filters to change the behavior of the event or change in the dataset, below are the available filters that can be used in WordPress.

List of Store Locator Filters #

FilterDescription
asl_filter_stores_resultThe purpose of the above filter is to change the dataset of the stores that are thrown to the store locator in JSON format.
asl_filter_locator_attrsTo change the shortcode attributes of the store locator, for example, you want to hide the list of the store locator via a hook, that can be performed with it.
asl_filter_locator_wordsIn case you want to change or replace any word text of the store locator that is injected in the JS file, it can be done via this filter without modifying any core file.
asl_filter_search_widget_wordsIn case you want to change or replace any word text of the Search widget can be done via this filter.
asl_parse_csvThis filter can be used to manipulate the data before it is imported via the CSV importer tool.
asl_woocommerce_store_settingsThis filter is being used in the Multi-Store Addons for WooCommerce before the Store data is saved through the form.
asl_filter_store_detailThis filter will be fired when the store detail widget is being used on some page.
asl_filter_store_formThis filter will be fired during the rendering of the Store Registration form, you can change the configuration of the form using it.
asl_filter_locator_configThis filter will be fired before the rendering of the Store Locator, you can tweak the configs using it.
asl_filter_pre_register_storeThis filter will be fired before the store registration form values are saved, so you can manipulate the data before saving it, since version (4.9.15)
asl_action_store_registeredThis action will be fired after the store registration form is saved, it provides the ID of the saved store, since version (4.9.15)
asl_before_stores_importAn action that executes before the import of the CSV file, you can delete existing stores as given in the example below, since version (4.9.15)
asl_filter_pre_insert_storeThis filter can be used to modify the store data before it is being inserted, since version (4.9.19)
asl_filter_pre_update_storeThis filter can be used to modify the store data before it is being updated, since version (4.9.19)
asl_filter_locator_categoriesThis filter can be used to manipulate the categories data for the store locator, available since version (4.9.15)
Filter Hooks Table

How to use the above filters? #

The above filters can be used in the functions.php file of your active theme, below is an example of the filter usage.

In this example, we have changed the category id to the category name, that is used in the query parameter.

/**
 * [asl_filter_locator_method description]
 * @param  [type] $attrs [description]
 * @return [type]        [description]
 */
function asl_filter_locator_method($attrs) {

	global $wpdb;

	$q_category = isset($_GET['locator-category']) && $_GET['locator-category']? $_GET['locator-category']: null;

	if($q_category) {

		$locator_category = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".ASL_PREFIX."categories WHERE category_name = %s", $q_category));

		if($locator_category && isset($locator_category[0])) {

			$attrs['select_category'] = $locator_category[0]->id;
		}
	}

	return $attrs;
};

add_filter('asl_filter_locator_attrs', 'asl_filter_locator_method');

Apply your own CSS file for the Search Widget #

/**
 * [asl_filter_search_widget_words_method Used this filter to change the CSS file]
 * @param  [type] $words [description]
 * @return [type]        [description]
 */
function asl_filter_search_widget_words_method($words) {

	//	Dequeue the plugin search css file
  wp_dequeue_style('agile-store-locator-asl-search');
  	
  //	Add your customized search file
  wp_enqueue_style('asl-search-widget',  get_template_directory_uri().'/asl_search-updated.css', array(), '4.9.12', 'all' );

	return $words;
}

add_filter('asl_filter_search_widget_words', 'asl_filter_search_widget_words_method');

Store Results Filter to Manipulate or Change the Store Data #


/**
 * [asl_add_additional_column Use this Method to add a new field to the data]
 * @param  [type] $all_stores [description]
 * @return [type]             [description]
 */
function asl_add_additional_column($all_stores) {

  foreach($all_stores as $store) {

    $store->agent = 'John Doe';
  }

  return $all_stores;
}

add_filter('asl_filter_stores_result', 'asl_add_additional_column');

Another example of asl_filter_stores_result is to through the data from some API or custom CPT.

/**
 * [asl_add_additional_column Custom data rendered using CPT or API endpoint]
 * @param  [type] $all_stores [description]
 * @return [type]             [description]
 */
function asl_stores_custom_data($all_stores) {

	//	Array of stores
	$my_stores = [];

	//	Add the Store 1
	//$my_stores[]

	//	Add the Store 2
	//$my_stores[]

  return $my_stores;
}

add_filter('asl_filter_stores_result', 'asl_stores_custom_data');

Truncate/Delete all my existing stores data before the import process

// Delete all my stores before the import of new files, Tested with version 4.9.15
add_action('asl_before_stores_import', function() {

	//	Create an instance
	$store_inst = new \AgileStoreLocator\Admin\Store();
	
	// Must be returned as object, else it will die
	$store_inst->as_object = true;

	//	Yes, delete everything, CAN'T be restored!
	$store_inst->admin_delete_all_stores();
});

Modifying the Store data on insertion/update #

Using the filter below, one can easily manipulate the store data before it is being saved, in the example we have removed the https:// on the website field.

// Pre-insert store method to make modification to the data
add_filter('asl_filter_pre_insert_store', function ($form_data) {
	
	// Remove "https://"
	$form_data['website'] = str_replace("https://", "", $form_data['website']);

  return $form_data;
});

The same method as above except this time it is for update store instead of insertion.

// Pre-update store method to make modification to the data
add_filter('asl_filter_pre_update_store', function ($form_data, $store_id) {

  // Remove "https://"
	$form_data['website'] = str_replace("https://", "", $form_data['website']);

  return $form_data;

}, 10, 2);

Perform WPML Translate to the Stores Categories via WPML #

The filter asl_filter_locator_categories can be utilized to translate the categories through string translation.

/**
 * [translate_asl_categories_via_wpml Translate ASL Categories via WPML]
 * @param  [type] $catgories [description]
 * @return [type]            [description]
 */
function translate_asl_categories_via_wpml($catgories) {

	//	Loop over the categories
	foreach($catgories as $sl_category) {

		//	Pass through translation string
		$sl_category->name = esc_attr__($sl_category->name,'asl_locator');
	}

	return $catgories;
};

add_filter('asl_filter_locator_categories', 'translate_asl_categories_via_wpml');

Run/Execute Shortcode in the Stores Listing #

In this example, we will explain how you can execute a shortcode in the stores listing, we will use wpdatatables, which is a well-known WordPress plugin.

  1. Create a custom field in the ASL Settings, and name the field table_id.
    add field
  2. Add the table ID for the store that you want to render the table.
    add table value
  3. Add this code to your functions.php
/**
 * [asl_add_additional_column Use this Method to run a shortcode]
 * @param  [type] $all_stores [description]
 * @return [type]             [description]
 */
function asl_add_shortcode_to_listing($all_stores) {

  foreach($all_stores as $store) {
 
    if($store->table_id) {
      $store->description = $store->description.do_shortcode('[wpdatatable id='.$store->table_id.' table_view=regular]');
    }
  }
  return $all_stores;
}
add_filter('asl_filter_stores_result', 'asl_add_shortcode_to_listing');

Manipulate Content of CSV File During Import #

The import process has a WordPress filter asl_parse_csv that can be used to change or manage the content at the time of import, such as you need to change the country name from US to United States from the country column, as the United States is the accepted country name from the list of countries.

/**
 * [asl_parse_csv_method Manipulate the CSV import data]
 * @param  [type] $csv [description]
 * @return [type]      [description]
 */
function asl_parse_csv_method($csv) {

	//	Get the rows
	$rows = $csv->getRows();

	//	Loop over the rows
	foreach($rows as &$row) {

		// Change the country name
		if($row[6] == 'South Africa') {
			$row[6] = 'France';
		} 
	}

	//	Put it back
	$csv->setRows($rows);

	return $csv;
}


add_filter('asl_parse_csv', 'asl_parse_csv_method');