Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeelia/e8157156751a021c89ff41c468ac5e4d to your computer and use it in GitHub Desktop.
Save felipeelia/e8157156751a021c89ff41c468ac5e4d to your computer and use it in GitHub Desktop.
Add to ElasticPress the capability to partially match a WooCommerce SKU.
<?php
/**
* Add the ngram analyzer if not added yet by autosuggest.
*
* @param array $mapping The mapping array.
* @return array
*/
function ep_custom_add_edge_ngram_analyzer( $mapping ) {
if ( ! @isset( $mapping['settings']['analysis']['analyzer']['edge_ngram_analyzer'] ) ) {
$mapping['settings']['analysis']['analyzer']['edge_ngram_analyzer'] = array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array(
'lowercase',
'edge_ngram',
),
);
}
return $mapping;
}
add_filter( 'ep_post_mapping', 'ep_custom_add_edge_ngram_analyzer', 11 );
/**
* Add custom SKU field mapping
*
* @param array $mapping The mapping array.
* @return array
*/
function ep_custom_add_sku_field( $mapping ) {
if ( version_compare( \ElasticPress\Elasticsearch::factory()->get_elasticsearch_version(), '7.0', '<' ) ) {
$post_mapping = &$mapping['mappings']['post'];
} else {
$post_mapping = &$mapping['mappings'];
}
$post_mapping['properties']['product_sku'] = [
'type' => 'text',
'fields' => [
'raw' => [
'type' => 'keyword',
'ignore_above' => 10922,
],
'suggest' => [
'type' => 'text',
'analyzer' => 'edge_ngram_analyzer',
'search_analyzer' => 'standard',
],
],
];
return $mapping;
}
add_filter( 'ep_post_mapping', 'ep_custom_add_sku_field', 11 );
/**
* Add the SKU value to the custom SKU field in the Elasticsearch document.
*
* @param array $post_args Post data.
* @param int $post_id Post ID.
* @return array
*/
function ep_custom_set_product_sku( $post_args, $post_id ) {
if ( 'product' !== $post_args['post_type'] ) {
return $post_args;
}
$sku = get_post_meta( $post_id, '_sku', true );
if ( ! empty( $sku ) ) {
$post_args['product_sku'] = $sku;
}
return $post_args;
}
add_filter( 'ep_post_sync_args_post_prepare_meta', 'ep_custom_set_product_sku', 10, 2 );
/**
* If weighting is in use, apply to the custom field the same behavior set for regular SKU.
*
* @param array $weight_config Weighting config.
* @return array
*/
function ep_custom_search_custom_sku_field( $weight_config ) {
if ( ! empty( $weight_config['product'] ) ) {
$weight_config['product']['product_sku.suggest'] = $weight_config['product']['meta._sku.value'];
}
return $weight_config;
}
add_filter( 'ep_weighting_configuration_for_search', 'ep_custom_search_custom_sku_field' );
/**
* If weighting is not use, adjust default values to use custom SKU.
*
* @param array $defaults Default values per post type.
* @param string $post_type Post type slug.
* @return array
*/
function ep_custom_search_custom_sku_field_by_default( $defaults, $post_type ) {
if ( 'product' !== $post_type ) {
return $defaults;
}
$defaults['product_sku.suggest'] = array(
'enabled' => true,
'weight' => 1,
);
return $defaults;
}
add_filter( 'ep_weighting_default_post_type_weights', 'ep_custom_search_custom_sku_field_by_default', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment