WooCommerce short-description

Hi, I need to index WooCommerce short-description field to make it searchable. How do I accomplish this?

My current code is as follows:

function filter_searchable_post_types($post_types) {
   $post_types = [];
   $post_types[] = 'post';
   $post_types[] = 'product';
  
  return $post_types;
}
add_filter( 'algolia_searchable_post_types', 'filter_searchable_post_types' );

function my_posts_index_settings( array $settings ) {
    $settings['attributesToIndex'][] = 'unordered(post_excerpt)';
    $settings['attributesToIndex'][] = 'unordered(postexcerpt)';
    $settings['attributesToSnippet'][] = 'post_excerpt:6';
    $settings['attributesToSnippet'][] = 'postexcerpt:6';
     
     return $settings;
}
add_filter( 'algolia_searchable_posts_index_settings', 'my_posts_index_settings' );

Best regards.

1 Like

Hello,

Your code snippets look good to me.
Did you re-index everything after implementing it?

What is the behaviour you have opposed to what you are expecting?

Speak soon,

I have changed my code to this:

$settings = array(
  'attributesToIndex' => array(
      'unordered(post_title)',
      'unordered(post_excerpt)',
      'unordered(content)',
  ),
  'customRanking' => array(
      'desc(is_sticky)',
      'desc(post_date)',
  ),
  'attributeForDistinct'  => 'post_id',
  'distinct'              => true,
  'attributesForFaceting' => array(
      'taxonomy_post_tag',
      'taxonomy_category',
      'post_author.display_name',
  ),
  'attributesToSnippet' => array(
      'post_title:20',
      'post_excerpt:20',
      'content:30',
  ),
  'snippetEllipsisText' => '…',
);

// Always return the value we are filtering.
return $settings;

And then I reindexed everything changed the index name prefix to get a clean start and the post_excerpt is being indexed. I made sure othat it works in Algolia Admin and it works.

The only problem now is that the Autocomplete wont work. The Instant Search made on the searchpage lists everything perfectly but the Autocomplete wont list products.

It does not search the post_excerpt. What am I missing @rayrutjes?

Hey,
The post excerpt is not displayed by default.

To do so you would need to customize the autocomplete template.

Let me know how that goes,

Hi @rayrutjes

Sorry for late reply, I have been on some holidays etc. I have altered the autocomplete template but cannot get it work yet.

You see an example of it here https://gist.github.com/damircalusic/fba5589a4dc08d1660bee42706fe7d62
What am I missing?

Thanks.

Do you have any JS error?
Can you confirm that the post_excerpt attribute is available in your Algolia dashboard?

No JS error and the post_excerpt is avalable in Algolia dashboard. As you can see the Instant search works with post_excerpt but the Autocmplete doesn’t with no JS errors.

What do you think @rayrutjes?

Are you still there @rayrutjes? :slight_smile:

Sorry about the late reply @damir.

According to your autocomplete screenshot, it looks like you need to enable the missing indices in order to see the results.

Can you confirm that the results you are seeing on the search page are also checked on ‚ÄúAlgolia Search -> autocomplete‚ÄĚ?

Hi @rayrutjes,

The settings look like this products
The post excerpt is in teh products so, do I need to activate something else aswell?

If I check searchable_posts then it works. But the problem is that I cannot section the results as this image displays


Is there a solution for this?

This is what you currently have and affects the searchable posts index.

You may also need the add_filter( 'algolia_posts_product_index_settings', 'my_posts_index_settings' );

Aha thank you @rayrutjes!! It worked now! :smiley:

1 Like