wp-search-with-algolia icon indicating copy to clipboard operation
wp-search-with-algolia copied to clipboard

Add WooCommerce SKU to index

Open liamhiggo opened this issue 5 years ago • 23 comments

Is your feature request related to a problem? Please describe. Not a problem. Just a matter for user experience.

Describe the solution you'd like I know there has been hesitation to add support for 3rd party plugins but I believe adding WooCommerce Product SKUs is something that falls outside of this scope. Already we can do product names, descriptions, and taxonomies but the SKU is also a unique identifier for a product.

Describe alternatives you've considered I tried implementing my own filters but the index was returning no results at all for products.

add_filter( 'algolia_post_shared_attributes', 'algolia_product_post_attributes', 10, 2 );
add_filter( 'algolia_searchable_post_shared_attributes', 'algolia_product_post_attributes', 10, 2 );
function algolia_product_post_attributes( array $attributes, WP_Post $post ) {
  if ( 'product' !== $post->post_type ) {
    return $attributes;
  }

  $product_ID = $post->ID;
  $product = wc_get_product( $product_ID );

  $attributes['sku'] = $product->get_sku();

  return $attributes;
}

Additional context I have limited knowledge of PHP.

liamhiggo avatar Aug 25 '20 02:08 liamhiggo

Your solution is right, just tell Algolia to use this field on searches, go to Configuration Tab and add that field for Searchable Attributes.

Another way could be:

add_filter( 'algolia_searchable_post_product_shared_attributes', function( $shared_attributes, $post ) {
	
	$shared_attributes['custom'] = [
		'sku' => get_post_meta( $post->ID, '_sku', true ),
		];
	
	return $shared_attributes;
	
}, 10, 2 );

edirpedro avatar Aug 27 '20 18:08 edirpedro

By the way, I noticed that Algolia won't work with Woocommerce without making too many adjustments for the filters. I quit! 😩

edirpedro avatar Aug 27 '20 20:08 edirpedro

Bom Dia, what do you need @edirpedro ?

Also it is best to use $product->get_sku()

ramonfincken avatar Sep 01 '20 09:09 ramonfincken

@ramonfincken Morning. Did you get this plugin to work with Woocommerce, with its default widgets filters? I noticed this plugin replaces the WP search and this crash the Woocommerce search.

edirpedro avatar Sep 01 '20 13:09 edirpedro

I set up a demo to check for loading times of the search. If I am not mistaken you can disable the normal search box behaviour from beeing overriden by algolia. ( normal, JS , backend search are the options ).

ramonfincken avatar Sep 11 '20 11:09 ramonfincken

Bump bump. Do we have any updates regarding this because as it stands, entering exact SKU does not find any results.

After reviewing the WP Plugin support forums I found this issue linked:

Plugin https://wordpress.org/plugins/wp-search-with-algolia/

Post with this git discussion listed https://wordpress.org/support/topic/woocommerce-sku-not-working/

KamGraphica avatar Feb 25 '21 16:02 KamGraphica

No update that I'm aware of, at least in regards to doing something officially with the core plugin.

tw2113 avatar Feb 25 '21 18:02 tw2113

@tw2113 Noted, seem the beta version now discontinued had the feature in place but its odd that they weren't carried over.

Unfortunate, that a core attribute for SKU is not implemented.

KamGraphica avatar Feb 25 '21 19:02 KamGraphica

Adding $shared_attributes['sku'] = get_post_meta( $post->ID, '_sku', true ); //Get sku from $product object to line 209 in class-algolia-posts-index.php and line 203 in class-algolia-searchable-posts-index.php will get SKU's into the algolia index. Then you just have to add it to searchable attributes in the algolia dashboard.

I'm looking into a survivable function that can be added to a themes function file.

seanvandermolen avatar Jul 29 '21 19:07 seanvandermolen

Hi @seanvandermolen, you should be able to use add_filter within your theme's functions.php file. I have a similar setup to index the post author avatar URL in my own theme. You should be able to do this:

add_filter( 'algolia_searchable_post_shared_attributes', function( array $shared_attributes, WP_Post $post ) {
	$shared_attributes['sku'] = get_post_meta( $post->ID, '_sku', true ); //Get sku from $product object

	return $shared_attributes;
}, 10, 2 );

Please note that depending on what version of PHP you're running, you may not be able to do this with an anonymous function.

peiche avatar Jul 29 '21 19:07 peiche

I'm running PHP 7.4. I tried the example above add_filter( 'algolia_searchable_post_product_shared_attributes', function( $shared_attributes, $post ) and that worked, but it puts it as a custom attribute instead of sku.

I'll test this and see if it works. I was worried that because it was a private function I'd have to do something out of the box. Doesn't sound like it.

seanvandermolen avatar Jul 29 '21 19:07 seanvandermolen

Hi @seanvandermolen, you should be able to use add_filter within your theme's functions.php file. I have a similar setup to index the post author avatar URL in my own theme. You should be able to do this:

add_filter( 'algolia_searchable_post_shared_attributes', function( array $shared_attributes, WP_Post $post ) {
	$shared_attributes['sku'] = get_post_meta( $post->ID, '_sku', true ); //Get sku from $product object

	return $shared_attributes;
}, 10, 2 );

Please note that depending on what version of PHP you're running, you may not be able to do this with an anonymous function.

Had to revisit this after an update. I tried this function and it didn't work. Not sure why you cant just include that one line in your codebase. If the end-user isn't running woo, they'd just have an empty attribute. With it, and you suddenly support Woo/Product searches well. Seems like a win win.

seanvandermolen avatar Oct 20 '21 16:10 seanvandermolen

Just to confirm, you have verified that the get_post_meta( $post->ID, '_sku', true ) line is fetching the intended data correct? Trying to help determine if this is an issue with the callback or the indexing afterwards

tw2113 avatar Oct 20 '21 17:10 tw2113

No, it does not fetch the data. I added the function, then re-indexed the search page and the autocomplete configuration and the SKU attribute was not visible in the Algolia dashboard. I went back and added the filter to class-algolia-posts-index.php in my original solution and it worked. The only problem is it doesn't survive updates.

edit-- well I might need to test again. because I did not add the line to class-algolia-searchable-posts-index and the sku attribute is there. I'm going to re-test and update shortly.

seanvandermolen avatar Oct 20 '21 18:10 seanvandermolen

@tw2113 I had a duh moment looking at the filter and realized it was only applying to searchable posts, which doesn't affect autocomplete. So I duplicated your filter and renamed it to target algolia_post_shared_attributes. This DID work! I imagine I can use the same filter to add Woo terms tags & categories the same way?

add_filter( 'algolia_searchable_post_shared_attributes', function( array $shared_attributes, WP_Post $post ) {
	$shared_attributes['sku'] = get_post_meta( $post->ID, '_sku', true ); //Get sku from $product object

	return $shared_attributes;
}, 10, 2 );

add_filter( 'algolia_post_shared_attributes', function( array $shared_attributes, WP_Post $post ) {
	$shared_attributes['sku'] = get_post_meta( $post->ID, '_sku', true ); //Get sku from $product object

	return $shared_attributes;
}, 10, 2 );

seanvandermolen avatar Oct 20 '21 19:10 seanvandermolen

Now my only problem is in my instantsearch.php template I was using {{{ data._highlightResult.sku.value }}} and that is returning an error Uncaught (in promise) TypeError: can't access property "value", data._highlightResult.sku is undefined

seanvandermolen avatar Oct 20 '21 19:10 seanvandermolen

I imagine I can use the same filter to add Woo terms tags & categories the same way?

I have to imagine you could get all that added all in the same callback, unless you want to keep those things separated out to a certain degree.

I wonder what all data is on the data._highlightResult portion, instead of specifically the .sku property

tw2113 avatar Oct 20 '21 21:10 tw2113

@tw2113 I'm not sure, is this the instantsearch.js doc or the Algolia PHP API Client /3.1.0 doc I'm reading?

All I know is that it worked with 2.01 and the bump to 2.1.0 broke it.

seanvandermolen avatar Oct 20 '21 23:10 seanvandermolen

Hmm. Not sure on that part myself either. @richaber do you have any ideas what may be going on with that part?

tw2113 avatar Oct 20 '21 23:10 tw2113

What is weird is that I'm using {{{ data._highlightResult.sku.value }}} in my autocomplete template and it's working just fine. I get the error only in instantsearch. I even have them sharing the same searchable_posts index now.

seanvandermolen avatar Oct 21 '21 02:10 seanvandermolen

I'm still believing in our previous stances of not directly adding 3rd party integrations right in the core plugin, as we have the hooks in place to get data into indexes and everything.

It's still in the works, but I do have a blog post mini-series planned regarding Algolia + WooCommerce integrations planned for the WebDevStudios blog. I also intend to consolidate all the code examples into a starter plugin for Algolia+WC that people can further customize to fit their own needs. One of the examples is going to include tested SKU indexing as well.

Leaving this open for the moment until we get those published.

tw2113 avatar Aug 16 '22 22:08 tw2113

I would argue that even though WC is 3rd party. It's such a highly used plugin in the WP ecosystem that it should be included or accepted as basically the defacto WP ecommerce solution. If the plugin detects WC is installed, it should include the SKU's and attributes automatically.

Any custom hooks and examples should really focus on things like custom fields and/or other 3rd party plugins or plugins that extend WC product custom fields, attributes, tags.

seanvandermolen avatar Aug 18 '22 19:08 seanvandermolen

@asharirfan what are your thoughts on this topic?

I'm still not convinced we absolutely automatically should index SKUs if WooCommerce is installed, but at the same time we do auto index a decent amount of post data for posts in general, which I assume are educated assumptions for what should be indexed.

I think a WooCommerce-specific add-on could be a good thing to create, and we could even create UI/Options pages for it as well, to save on people needing to code up a lot.

tw2113 avatar Aug 22 '22 03:08 tw2113