This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Integration with Fluxstore app technical question

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Integration with Fluxstore app technical question Reply To: Integration with Fluxstore app technical question

#37760
Ernest MarcinkoErnest Marcinko
Keymaster

Okay, I will try to help with this as much as possible. Unfortunately the plugin does not have a rest API yet, but we have a documentation on adding custom endpoints via the WP Rest API.

The main issue with integration is, that users can create multiple search instances, each with different configurations – and it is very much possible to have multilpe search bars configured differently for woocommerce, as well as search bars not searching WooCoomerce at all at the same time.
Doing a “generic” request to do a search is not going to work, the specific search bar has to be specified by ID.

To simplify this, I constructed this REST endpoint, which tries to “auto detect” the search instance created for WooCommerce. This exact endpoint will be implemented in a future release. If you add this to your theme functions.php file:

function asp_custom_rest_handler( $data ) {
	$id = -1;
	$defaults = $args = array(
		's' => ''
	);
	foreach ( $defaults as $k => $v ) {
		$param = $data->get_param($k);
		if ( $param !== null ) {
			$args[$k] = $param;
		}
	}

	// Fetch the search ID, which is probably the WooCommerce search
	foreach ( wd_asp()->instances->get() as $instance ) {
		if ( in_array('product', $instance['data']['customtypes'] ) ) {
			$id = $instance['id'];
			break;
		}
	}

	// No search was found with products enabled, set it explicitly
	if ( $id == -1 ) {
		$args['post_type'] = array('product');
	}

	$asp_query = new ASP_Query($args, $id);
	return $asp_query->posts;
}

// POST to: http://example.com/wp-json/ajax-search-pro/v1/woo_search
add_action( 'rest_api_init', function () {
	register_rest_route('ajax-search-pro/v0', '/woo_search', array(
		'methods' => 'POST',
		'callback' => 'asp_custom_rest_handler',
	));
});

Then the usage is:

curl --location --request POST 'https://test.local/wp-json/ajax-search-pro/v1/woo_search?s=test'

..or postman link.

We are planning to add this exact endpoint as well to a future release – so they can safely implement requests to it now if they want to, it will be compatible.