Add WooCommerce SKU to index
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.
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 );
By the way, I noticed that Algolia won't work with Woocommerce without making too many adjustments for the filters. I quit! 😩
Bom Dia, what do you need @edirpedro ?
Also it is best to use $product->get_sku()
@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.
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 ).
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/
No update that I'm aware of, at least in regards to doing something officially with the core plugin.
@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.
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.
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.
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.
Hi @seanvandermolen, you should be able to use
add_filterwithin 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.
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
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.
@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 );
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
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 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.
Hmm. Not sure on that part myself either. @richaber do you have any ideas what may be going on with that part?
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.
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.
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.
@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.