Developer

This document is developer level. You’ll need to understand PHP to extend Availability Search for WooCommerce Bookings.

Access the search data via PHP

Use our aswb_before_processing_search_request hook to access the search from each Availability Search request. The below function is a starter example that shows you how to access each piece of information. This code will run every time someone runs the availability search.

/**
 * Function to access the search date for each ASWB search request.
 *
 * @param [type] $raw_request
 * @return void
 */
function custom_aswb_before_processing_search_request( $raw_request ) {

	// Uncomment to view request structure in your error log.
	// error_log( print_r( $raw_request, true ) );

	$shortcode_settings = $raw_request['attributes']; // These are the settings of the shortcode.

	$start_time = $raw_request['start_formatted_time']; // 2020-11-20 0:00
	$end_time = $raw_request['end_formatted_time']; // 2020-11-20 0:00

	$keyword = ! empty( $raw_request['keyword'] ) ? $raw_request['keyword'] : false;

	// error_log( print_r( $keyword, true ) );

	// Requested Page. You might not want to track page 2++.
	$paged = ( isset( $query_data['paged'] ) ) ? intval( $query_data['paged'] ) : 1;

	// Custom taxonomies by their name and find the terms ids.
	if ( ! empty( $raw_request['taxonomies'] ) ) {
		foreach ( $raw_request['taxonomies'] as $key => $value ) {
			$tax_name = explode( 'aswb-taxonomy-', $key );
			$tax_name = $tax_name[1]; // eg product_cat.
			// error_log( print_r( $tax_name, true ) );

			$term_ids = $value; // ids as array.
			// error_log( print_r( $term_ids, true ) );
		}
	}

	// Do something with the data...
}

add_action( 'aswb_before_processing_search_request', 'custom_aswb_before_processing_search_request', 10, 1 );Code language: PHP (php)

It’s completely up to you what you want to with the data.
If you are looking to send search data to Google Analytics Event tracking via PHP – have a look at this example https://gist.github.com/chrisblakley/e1f3d79b6cecb463dd8a

Changing the default search configuration

You can change parts of the default search configuration using the below filter. In the example only the search_on_date_change is disabled to prevent the datepicker from automatically searching when dates are changed. Users will have to click on the dedicated “Search” button after selecting their dates.

/**
 * Disable the automatic search on date changes.
 */
function custom_aswb_change_search_config( $config ) {

	$config['search_on_date_change'] = false;

	return $config;
}

add_filter( 'aswb_filter_default_search_configuration', 'custom_aswb_change_search_config' );Code language: PHP (php)

Modifying WP Query Args Before Searching

/**
 * Example: Modify the bookable products query using the 'aswb_wp_query_args' filter.
 *
 * This adds a filter for when a specific [availability_search id="example"] shortcode is used.
 */
add_filter( 'aswb_wp_query_args', function( $args, $query_data ) {
    
    // Only apply if the specific ID is passed from the shortcode
    if ( empty( $query_data['id'] ) || $query_data['id'] !== 'example' ) {
        return $args;
    }

    // Add a product category filter (e.g., 'bookable-products')
    $args['tax_query'][] = array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => 'bookable-products',
    );

    // Add a meta query filter (e.g., custom field _is_bookable = yes)
    $args['meta_query'][] = array(
        'key'   => 'example_meta',
        'value' => 'yes',
    );


    return $args;
}, 10, 2 );
Code language: PHP (php)

Was this page helpful?