Support » Plugin: WP Search with Algolia » Add Product info to wp_searchable_posts Indices

  • Resolved rollies


    Hi All

    Im trying to add Product data like Sku and Price etc to the “wp_searchable_posts” Indice so I can display it on the instantsearch.php, I have been able to get that working on the autocomplete.php, but not the instantsearch.php

    Adding the below

    $product = wc_get_product( $post->ID ); //Get product from $post object
    $shared_attributes['sku'] =  $product->get_sku(); //Get sku from $product object

    to “class-algolia-post-index.php” as suggested here works but only for the autocomplete-

    trying to add it to the “class-algolia-searchable-posts-index.php” stops the “All Posts from indexing”.

    Any help would be great


Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Michael Beckwith


    The BenchPresser

    With regards to it stopping, I’m wondering if you are encountering an error that interrupts the process.

    With regards to getting these added to the spot as a whole, you shouldn’t be editing the plugin files themselves. I’ve double checked and there are these two filters that you should be using instead to add your WC data:

    $shared_attributes = (array) apply_filters( 'algolia_post_shared_attributes', $shared_attributes, $post );
    $shared_attributes = (array) apply_filters( 'algolia_post_' . $post->post_type . '_shared_attributes', $shared_attributes, $post );
    Thread Starter rollies


    Great, Thank you

    • This reply was modified 5 months, 3 weeks ago by rollies.
    Thread Starter rollies


    Below is working code i have used if anyone needs it – added to themes function.php

    /* Algolia customisations */
    function add_product_shared_attributes( array $shared_attributes, WP_Post $post ) {
        $product = wc_get_product( $post );
        $shared_attributes['price'] =  $product->get_price();
        $shared_attributes['regular_price'] =  $product->get_regular_price();
        $shared_attributes['sale_price'] =  $product->get_sale_price();
        $shared_attributes['is_on_sale'] =  $product->is_on_sale();
        $shared_attributes['sku'] = $product->get_sku();
        $shared_attributes['stock_quantity'] =  $product->get_stock_quantity();
        return $shared_attributes;
    add_filter( 'algolia_post_product_shared_attributes', 'add_product_shared_attributes', 10, 2 );
    add_filter( 'algolia_searchable_post_product_shared_attributes', 'add_product_shared_attributes', 10, 2 );


    Thanks for sharing that code snippet.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.