Barcode image from Allan on flickr https://www.flickr.com/photos/misteral/

Search by SKU for Woocommerce

Download: Search by product SKU v 0.5

I recently completed a project that had a requirement for site users to be able to search for woocommerce products by the SKU (Stock keeping Unit). I looked at using relevanssi search but the configuration was a headache and seemed a little overkill to add the search for something very specific.

Plugin Description: The search functionality in woocommerce doesn’t search by sku by default. This simple plugin adds this functionality to both the admin site and regular search. If a product variation is matched the parent is returned otherwise it will try and match on a regular product stock keeping unit.

I don’t have any plans to extend the functionality to search any other aspects of wooommerce apart from the sku but feel free to use the plugin as a base for your own woocommerce search plugins – a link to this blog post would be appreciated though.

Changelog

Version 0.5

  • Improved support for sites running wpml for translations in woocommerce

Version 0.4

  • Remove hidden products from search results
  • “Total Found” search count works in more themes.

Old Versions

If anyone wants to revert back to an older version of the plugin at any point they are still hosted here: http://code.mattyl.co.uk/wordpress-plugins/searchbysku/old/

If you have used any of my plugins successfully why not throw me a few $ here:

(Photo Credit Allan on Flickr https://www.flickr.com/photos/misteral/)

103 thoughts on “Search by SKU for Woocommerce”

    1. Hi Anto.

      This plugin only searches by SKU but I might release another simple one that will do a search on the product tag. It should be a fairly simple one and sounds useful.

  1. Hey thanks for the plugin.

    It seems to work well when the complete, correct sku is searched. But if searching for a partial sku, it only shows the first result that matches, instead of all products that have sku’s starting with the partial search. For example:

    Search for “PHA01″

    Results show: PHA012
    Results SHOULD show: PHA012, PHA015

    Is this possible? Thanks,

    Jeff

      1. Hi Jeff,

        i know its been a long time coming but I have finally got round to supporting the partial SKU matches. Hopefully this is still relevant for your project.

        -Matt

  2. Hi, great plugin.
    Will this as well display the items in the catalogue sorted by SKU (which is something I’m trying to achieve)

    Thanks & best regards
    Katharina

  3. LOVE IT MATT !!!
    Simple, clean, & does exactly as it claims!

    Thank you so much for sharing the fruits of your labor!

    Sincerely,
    Freddy

    P.S.: Bookmarking your site and will definitely spread the word!

    1. Thanks for the kind words Freddy. I’m currently trying to move a couple plugins to wordpress.org so keep your eyes out for those :p

  4. Hi Matt,
    Yur pluginis good but you don’t have need plugin.
    In the search bar admin product tipping SKU: +your sku.

    Recherche de “PHA01″

    Les résultats montrent: PHA012
    Les résultats doivent montrer: PHA012, PHA015

    Est-ce possible? Merci,

    Jeff

    Matthew Lawson
    Matthew Lawson 21 décembre 2012 à 11:48 | Lien permanent | Répondre

    Salut Jeff, heureux vous l’avez trouvé utile.

    Je vais jeter un oeil à ce que devrait être assez rapide pour le changer.

    -Matt

    Have you found for more result ?

    1. Hi Mike,

      This will work in the front end of a site too without the need for a user to prefix their search with “sku:’.

      I still have plans to move these plugins to wordpress.org when I get a chance. When I do I will look at the partial matching of an SKU too that Jeff mentioned.

      -Matt

  5. Though I see you still intend to work on the ‘partial sku’ portion, just wanted to let you know when I used the plug-in and tried a ‘partial’ sku search I always received back php garbage on the screen. When I deactivated the plug-in, woocommerce went back to displaying the usual “No products found which match your selection” screen.

    I look forward to seeing this up on WordPress.org. I’ll definitely give it a positive vote with the partial sku search working. Thanks for your time coding this!

  6. Hi Matt – after I updated to WooCommerce 2+ this no longer works on front end. I keep getting a white screen when I search. Works in product admin end ok.

  7. RE: my last comment – it seems I was searching for out of stock skus (hidden from catalogue). works ok for in stock items.

      1. I’ll take a look, previously some people were experiencing duplicates caused by the wpml plugin. Are you using any other translation plugins that might be making duplicates of products in the database?

  8. Hey Aclase,

    I had a product inventory (750+ lines) with SKU’s in the title, so also had duplicate results but I resolved it by stripping them out of the titles and used a filter on the WooCommerce product title to add the SKU back in dynamically.
    Now the store admin doesn’t need to worry about adding the SKU to the title manually and no more duplicate results (although Matthew’s fixed that now anyway).

    Let me know if this might be of interest to you too.

  9. Thank you I been looking for a search plugin that will search for SKU, I gave up I actually placed the sku number on the title of the product, which I hated but it actually worked on the search.

    Thanks for doing this!!!!! Now I can remove the SKU from the title name :D

    1. Mimi, have you considered the SEO impact of not having the SKU in the title? Just asking as I often start a Google search with a product #.

  10. Hi Matthew,
    First, thanks for the great plugin!
    I’ve encountered a weird behavior. On the orders page, when I search by sku it pulls up the right number of rows but they are blank. For example, I search for sku 00123 and the results shown are 1 empty row. There is only 1 order with this sku – so the number of rows is correct. However, is it possible to show these results?

    Thanks,
    J

    1. Thanks for the feedback. There has been a bit of a wave of interest in this plugin recently so I will take some time soon to investigate this and move the hosting to wordpress.org. Is it possible the mismatch in products displayed and the ‘total found’ is caused by some of the products being hidden ?

  11. Matthew,
    Does exactly what it says on the tin! Nice one, Sir!

    Thanks very much for sharing this. I really don’t know why this functionality is not enabled by default in the Woocommerce product search. So thanks again for sharing your great plugin.

    Are you going to share this on WordPress.org?

    1. Thanks Rich and also thanks for the mention on your blog.

      I have all the repo details sorted for wordpress.org I just need to stop being sidetracked and take some time to stick it up there. I’ll update the post when its on wordpress.org.

  12. Hi

    Thanks for the pluggin! I’m developing a Woo-com store and migrating our Drupal store over to WP.

    After the initial setup I’m seeing we have a lot of custom features not offered in WP. The main one being an admin option of sorting ONLY ordered products by SKU, this is to create lists of products to order from our various suppliers.

    My question is, is there already a way to do this? If not how do other stores process orders when they have multiple suppliers? And finally is there a way to modify your pluggin to sort only “ordered” products by SKU?

    Thanks!

    1. Hi Scott,

      That is a little outside the scope of what this plugin does. It is purely for enhancing the search functionality to include products if their SKU matches the search query.

      It sounds like you want to add another report to the reports tab that do a query for all the ordered products sorted by SKU. Alternatively the Woo team are quite good at adding filters to most of their output so you might be able to use that filter to manipulate the output of an existing report into doing what you want.

  13. Hey Matthew! I am looking for a plugin that will show the SKU # on my sales log. Would this cover me?

    Thanks!

    1. Hi Rachel,

      This plugin is only for adding products to the search results if they match the users query. I haven’t done what you are trying to do before but if you find the sales log view in the woocommerce code there may be a filter / action you can hook onto to manipulate the output into adding the Stock Keeping Unit.

    1. I have thought about making a more generic woocommerce turbo search that would include attributes but haven’t had the time yet. I believe the data is structured similarly in WC 2.0 so it shouldn’t be too much of a stretch to get a plugin working that will search by attributes. Performance wise its not so good as to do the search in mysql a couple tables need to be joined, thats why a there is a couple generic wordpress search plugins that build their own indexes. These are fairly hard to configure for woocommerce too as they require knowledge of the underlying data structures.

      For the small – mid size store the plugins I do should be fine though.

  14. Hi Matthew,

    Thank you for the plugin.
    It was working properly before, but I’m facing a problem.
    Each product has three variations based on sizes and has its own SKU. One product, for example, has three variations, and SKUs are CL 001, CL 002, and CL 003. When I searched by CL 003, it shows the product, but does not show the specific size.
    I’m using woo commerce version 2.0.13. I reinstalled your plugin.

    I need to provide a custom agent a searchable SKU number. When they enter a SKU number, they are expecting to see the particular product, size and price.

    I’m wondering I’m missing something.
    Thank you for your help.

    Best

    1. Hi,

      That was a deliberate decision i made so that if a variation is matched by SKU it will return the parent product in the search listings and not the individual product.

      It was how I needed it for a project I was working on so I left it that way. I’m pretty sure its how woocommerce store works too you can’t browse the store and go directly to a size / weight of a product you enter through the parent and select the variations you want from there. It might be grouped / simple products that you need rather than a variable product.

      Hope that helps,

      -Matt

  15. Hi Matthew,

    Thanks for the plugin, it works just fine. But when searching by SKU on a multilangual shop it displays all translated versions of the product and not the one of the current language

    I’ve already added the Relevanssi plugin to limit the search results on the current language but that doesn’t help either when only searching for the SKU.

    Is there a way to display only the results bases on the current language?

    Thanks,
    Niels

    1. Hi Niels,

      Multi language products aren’t supported by default with Woocommerce. I presume this is another 3rd party plugin to add multi language functionality to your store? Depending how that works it might create multiple products in the database with duplicated tags, but I’m just speculating. I haven’t tested how it behaves with any other 3rd party woocommerce plugins enabled.

  16. Thank you for the plugin.
    Works perfect (woocommerce 2.0.14, wordpress 3.6.1, Search by Sku Version 0.4 ).
    The only thing that could need to upgrade if possible is in cases of multilingual sites, using wpml, to return the results in the correct language.
    Using wpml you have double products with the same sku so it results in showing two products with the same sku but they are two products in two different languages. How can this be filtered somehow?

    Thank you again!

    1. I will take a look at this for version 0.5 as a couple of people have mentioned it now. No promises of timescale I’m not currently working on any woo commerce sites.

  17. Thank you for this plugin!!! Really like the fact that the user does not have to know the exact SKU.

    The search result page after activating plugin does not appear to activate or integrate with the WooCommerce Layered Nav widget (filtering / narrowing results). After digging into the widget code and your code I think I know why. You use the global variable $posts. WooCommerce Layered Nav widget uses the variable $woocommerce->query to check if the resulting products has any of the terms in the selected attribute of the widget. If the resulting products do not have any of the terms, it does not show the widget. So, for debugging purposes, I manipulated $woocommerce->query at the beginning of their loop like so:
    foreach ($posts as $post)
    if ($post->post_type == ‘product’)
    $woocommerce->query->unfiltered_product_ids[] = $post->ID;
    Basically adding the product ids in your $posts into their $woocommerce->query. Now the widget appears.

    Not sure what is the right way to enhance your plugin so that it can be use in conjunction or integrated with WooCommerce Layered Nav widget. Could you recommend how to fix this?

    Thanks,

    Luke

  18. Hi Matthew
    Congratulations for this plugin, it works well.
    there is a way to search by category products?

    i tried searc_everything plugin
    but there is a problem…
    the shopbar sidebar doesnt appears…
    if i write in the search box a name of a category, the producs on this category appears correctly but the shop side bare on the left side no…
    maybe you know a little “trick” to fix this problem?

  19. I had the same issue regarding wpml, so I threw together a very crude hack.
    It’s not that elegant, but seems to work in my scenario. Maybe it helps someone

    in

    function get_parent_post_by_sku($sku, $ignoreIds) {

    right after

    global $wpdb, $wp_query;

    i added

    /**create additional wpml query if it’s defined**/
    $wpmlQuery=”;
    if(defined(‘ICL_LANGUAGE_CODE’)){
    $wpmlQuery=” join “.$wpdb->prefix.”icl_translations wpml on p.ID = wpml.element_id and wpml.language_code=’”.ICL_LANGUAGE_CODE.”‘ “;
    }

    then , after both
    visibility.meta_value ‘hidden’

    i just added

    $wpmlQuery

    so it reads

    and visibility.meta_value ‘hidden’
    $wpmlQuery
    where 1

    No guarantees, but maybe it works for someone

  20. actually, it appears to be something along these lines

    /**create additional wmpl query if defined**/
    $wpmlQueryVariations=”;
    $wpmlQuery=”;
    if(defined(‘ICL_LANGUAGE_CODE’)){
    $wpmlQueryVariations=” join “.$wpdb->prefix.”icl_translations wpml on p.post_parent = wpml.element_id and wpml.language_code=’”.ICL_LANGUAGE_CODE.”‘ “;
    $wpmlQuery=” join “.$wpdb->prefix.”icl_translations wpml on p.ID = wpml.element_id and wpml.language_code=’”.ICL_LANGUAGE_CODE.”‘ “;
    }

    and then use the relevant var in the relevant query

    1. Hi Olly, thanks for this.

      I added support for wpml in a 0.5 release this morning and looking at your comment again, the join I added is pretty much identical with slight difference for the variations query.

      -Matt

      1. Hi Matt,

        thanks for that update.

        There does however seem to be another issue (unrelated to WPML though)

        lets say – for arguments sake – I have a simple product with sku 123

        I furthermore have another simple product with sku 456

        at some time in the past, product 456 was a variable product where one of the variations had sku 123 too

        at some point however, product 456 was converted / set back to a simple product (i .e the variations are not displayed anymore – the are however still postmeta table as one might want to revert back to that and woocommerce does therefore not delete this data)

        if I now search for sku 123 it still finds both products
        i.e product 123 – which is fine – but also product 456, is it still has the variations stored in the postmeta table although they are not active.

        i had a look around if there’s a flag that tells me if a variation is active or not, but somehow cant figure it out …

        do you have any ideas how i might get around that one ?

        1. so, having hacked around yet a few hours more , the following *seems* to address this issue.
          as ever, there’s probably a better way to write/do this, but – for the moment at least – it seems to work for me (as always, no guarantees though)

          essentially, i changed

          foreach($variations as $post)
          {
          //var_dump($var);
          $ignoreIds[] = $post->post_id;
          }

          to

          foreach($variations as $k=>$post){
          /**if it’s a SIMPLE product, but it has variations set for some reason to maybe revert to in the future (although they would not be displayed whilst being a “simple” product) , exclude it from the variation query result*/
          $productTterms = wp_get_object_terms( $post->post_id, ‘product_type’,array(‘fields’ => ‘slugs’) );
          if($productTterms[0]==’simple’){
          unset($variations[$k]);
          }
          $ignoreIds[] = $post->post_id;
          }

          dunno, maybe it helps someone

          1. Hey,

            Thanks for pointing that out. It’s strange woocommerce doesn’t clean up after itself if you change a product from variable to simple. I will make a note of this for version 0.6 although I suspect its a fairly narrow use case. I will probably do it in the sql query to ensure its only variable products being retrieved.

            Cheers,

            -Matt

          2. i agree, woocommerce does strange things sometimes. i guess the point is that one can easily revert to the variable product again (however, why they dont set a flag in the postmeta table though is beyond me. after all, they are already putting 5 million meta_keys in there already per product……oh well)

            >I will probably do it in the sql query….
            probably the best idea (i just couldn’t be asked)

            >although I suspect its a fairly narrow use case…
            one would hope so, but then again, my current client is somewhat exuberant in messing around with things …

  21. Hi

    Been using this for a while with great success, how does it play with the latest WP update 3.7, as i understand that the search fuctions in wordpress have had a big overhaul.

    mark

    1. Hi, I haven’t tested this with the new version yet. But have not had any adverse feedback yet.

      Its on my list of things to check out.

  22. There is a small problem using this with the latest versions of WooCommerce and WordPress.

    I can still search by SKU, which is good, but if I use the “select a category” filter (on its own, without any search text) it seems to show all products rather than just the ones in that category (the ones in that category show up first though).

    Thanks
    Shaun

  23. Hi there,
    Your plugin has been a lifesaver! Unfortunately I’m having a little trouble with duplicate results. I’m using WooCommerce in two separate instances for the same website (on separate WP installations). The two instances are configured almost identically, except one is a members-only exclusive area for placing orders and one is a catalogue where anyone can see (but not buy) our products.

    The search works perfectly in the catalogue, but I get duplicate results in the ordering part of the website. I see products A, B, C and D once, then again in the same order: A, B, C, D. I’m using both Search by SKU and Search by Tag. When I deactivate both at the same time, the problem is fixed, but when I have either of them individually or both of them simultaneously active, I get duplicates.

    Any idea what is going on?

    1. I take it you mean when using the plugin in the admin site? Thats a good idea I think I will modify the plugin to include pending and drafts when searching from the admin site

  24. Hi,

    Really nice and usefull plugin.

    I have an issue with it. When I make a search the Items with the SKU are added to t2he main menu of the site as part of the menu. For example: If I search “AAA-BB and I have this two items AAA-BB and AAA-BBC the search result page shows both results and both are included on the main menu this way –> HOME ABOUT US CONTACT AAA-BB AAA-BBC breaking the layout of the header.

    I hope you know what I mean.

    How can I fix it?

    Thank you

    1. This sounds like it may be a theme using the WordPress loop in a way I wasn’t expecting. What is the theme is it avaable online anywhere?

  25. Hi,
    Fantastic plugin!
    I have added in a taxonomy for different locations and i was just wondering which parts of this plugin I could change to search by the locations instead or if this is even possible without rewriting the whole plugin?

    Many thanks,
    Jimmy

  26. Hello!

    Nice plugin, but i have a little issue.

    Let-s say i have sku: FLR-124 78
    if i search exact this, all perfect no problem
    when i search FLR 124 78 no result. Any ideea or fix?

    Thanks!

    1. Hey the plugin doesn’t really do fuzzy searches its all powered by a beefy mysql query its not a full engine that can do those “Did you mean” type suggestions

  27. Hey,

    Plugin is working perfectly out of the box with WordPress Version 3.9.1.

    However, I am currently using the SKU on variable products. After entering a search SKU, the relevant product page is displayed but no variation is selected.

    Is it possible that when a customer enters an exact-matching SKU that it opens up that product page with the relevant variation already pre-selected?

    Cheers.

    1. Hey im not sure I will have to investigate. The redirection to a specific product when one search result is returned is a woocommerce behaviour

  28. Would it be possible to alter the plugin code to show hidden pending products in sku search results in the dashboard, while only returning published results on front end searches? Thanks!

  29. Thanks for this plugin. However, I’ve noticed a bug. When you search for an SKU, the results list also displays on the Wooocmmerce widget for ‘recently viewed products’. So now I’ve got the results on both the main section and the sidebar.

    1. Thanks for that Jo, I’ll take a look. Either myself or woocommerce must be abusing the wordpress loop (probably me)

  30. Hi matt,

    Your plugin seems to do the job however when returning results on my search page its seems to effect the page layout by pushing my title bar down? any ideas if this could be fixed (prob just a css issue?)

    thanks
    adam

    1. I have had people feedback before than it sometimes effects the main menu but I haven’t been able to track the issue down. Which theme are you using?

  31. I have had to turn off this very useful plugin as it has started to interfere with product sorting in admin.
    Since WP 4 update.
    Any chance you can have a look at this?

    1. Hey thanks again for the donation and invaluable feedback. I have just released 0.5.2 which addresses the backend filtering issues more robustly than 0.5.1

  32. Hello!

    Your plugin was a lifesaver !. I built this website for a client, and they wanted to be able to search by SKU through both the admin panel, and the “product search” on the actual website. It worked beautifully.

    BUT, I actually bumped into a problem though… Whenever the client tries to filter the products by product category(within the admin panel > PRODUCTS>), the result seems to be correct at first. The first 20 results. Basically, the first 20 products on each page ( 3 pages ) DO correspond to the filtered category. Once you scroll past the first 20 items, you start seeing results from other categories. It’s showing ALL the 600+ products that we have – not just the 52 that are in the filtered category. It’s showing an abnormal amount of results per page ( 300-ish).

    Now, I’m positive that this plugin is causing this problem. I’ve de-activated the theme, and ALL the other plugins, but Woocommerce. Double-checked by activating this plugin and re-activating it. This plugin is definitely the culprit.

    Is there a line of code that I can insert into the PHP to fix this? I really want to continue using this plugin. Any suggestions?

    Woocommerce 2.2.3
    WordPress 4.0
    Search By SKU – for Woocommerce 0.5

    Thank You!

  33. Hi Matt,

    Great plugin, i am using the latest version from wp repo, however one small thing that its not doing from what i can see.

    If you have variable products with SKU’s this cannot be searched.

    I tried it from admin and it does indeed not work, however it does work from front end…

    So in the front end i search eg.. 114 a partial match it shows 2 results, in the backend however it only shows 1.

    Any ideas on how to overcome this buddy ????

    1. Hey thanks for the feedback, really makes it much easier for me to spot stuff like this cheers. I have just release 0.5.2 which addresses this problem :) Check it out.

  34. Hi Matthew,
    I just updated ‘Search by SKU’ from version 0.5 to 0.6. I am running WP 3.7.1. with Retailer theme. Website is leftcoastsupplies.com.

    It broke my search function, seemingly totally. It had been working flawlessly before that. Really nice plugin.

    Here is the error message I get:
    Fatal error: Call to undefined function wc_clean() in /home2/coast/public_html/wp-content/plugins/search-by-sku-for-woocommerce/woocommerce-searchbysku.php on line 41

    Could you please help…. this is a major website with over 2500 products.

    Really appreciate it,

    Tim

    1. Ah it looks like that function is in a newer version of woocommerce. I will find out exactly what version that was added. I will add a fallback to my plugin but in the time being if you update woocommerce to the most recent version that will also fix your issue.

  35. Hi Matthew,

    U have been using search by SKU with no problems until I updated to 0.6. Now it doesn’t work at all. I have had to revert back to 0.5 as this was all I could find. Am also using Relevanssi as my search, so not sure if there was a conflict here too. Any ideas?

    1. Hi thanks for using it, It’s possible there could be compatibility issues with relevanssi, I will have to investigate. Which version of woocommerce are you using?

  36. Hello!

    I have a question about your WONDERFUL plugin.

    The plugin was working beautifully up until a few weeks ago, but then I ran some updates and suddenly there is a strange thing happening.

    When I use the search box on my website (www.pearlsinternational.com), the live search results are showing up “behind” the page–they are only visible on the far edge of the screen where my theme’s text box is not visible.

    Do you happen to know if this is an issue with theme interference or if it could be an easy fix? I’m not an expert by any means, I know just enough to get myself in trouble. :)

    I’d appreciate any thoughts you might have on the subject. Thanks for your time!

    Sophie

    1. Hey thanks,i’m glad you found the plugin useful. I’d say its more likely to be a css problem either with the live search widget you’re using or the theme itself. My plugin doesn’t make any changes to the styling.

      Good luck tracking it down!

  37. Hey Brian, I have an update coming in a few weeks for this plugin. If you wanted to do it yourself in the time being you can find the part of the query that checks the published state of the product and add a conditional is_admin() check around it.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>