Algolia

<?php

// see: https://www.algolia.com/doc/integration/wordpress/getting-started/quick-start/?language=php

// Adding custom data to product post-type for Algolia Indices
add_filter('algolia_post_product_shared_attributes', function($attributes, $post) {
  $product = wc_get_product($post->ID);
  $attributes['sku'] = get_post_meta($post->ID, '_sku', true);
  $attributes['stock'] = get_post_meta($post->ID, '_stock', true);
  $attributes['stock_status'] = get_post_meta($post->ID, '_stock_status', true);
  $attributes['prices'] = [
    'price' => strip_tags(wc_price( wc_get_price_including_tax($product) ))
  ];
  if($product->is_on_sale()) {
    $sale_price = wc_get_price_including_tax($product, ['price' => $product->get_sale_price()]);
    $old_price = wc_get_price_including_tax($product, ['price' => $product->get_regular_price()]);
    $attributes['prices']['sale_price'] = strip_tags(wc_price( $sale_price ));
    $attributes['prices']['old_price'] = strip_tags(wc_price( $old_price ));
  }

  // clean algolia data sent by the plugin
  if(isset($attributes['post_author'])) {           unset($attributes['post_author']);          }
  if(isset($attributes['post_date_formatted'])) {   unset($attributes['post_date_formatted']);  }
  if(isset($attributes['post_mime_type'])) {        unset($attributes['post_mime_type']);       }
  if(isset($attributes['post_type'])) {             unset($attributes['post_type']);            }
  if(isset($attributes['post_type_label'])) {       unset($attributes['post_type_label']);      }
  if(isset($attributes['post_modified'])) {         unset($attributes['post_modified']);        }
  if(isset($attributes['post_date'])) {             unset($attributes['post_date']);            }
  if(isset($attributes['comment_count'])) {         unset($attributes['comment_count']);        }
  if(isset($attributes['menu_order'])) {            unset($attributes['menu_order']);           }

  return $attributes;
}, 99999, 2);

// Set additional search parameters for product post type
add_filter('algolia_posts_product_index_settings', function($settings) {
  $settings['attributesToIndex'][] = 'sku';
  // $settings['attributesForFaceting'][] = 'sku';
  $settings['attributesToSnippet'][] = 'sku';
  // $settings['disableTypoToleranceOnAttributes'][] = 'sku';
  unset($settings['attributesForFaceting']['post_author.display_name']);

  return $settings;
}, 10, 1);

// custom search on Indice for Algolia search-result page
add_filter('algolia_native_search_index_id', function() {
  return 'posts_product';
});
Last Updated:
Contributors: Niek Vlam, Suite Seven