Add Product SKU to the WooCommerce Search

18 Shares

The default product search is quite basic in WooCommerce, and unless you’re using a plugin like SearchWP to enhance it, your customers may not be finding the products they want. One simple enhancement you can make is to add the product SKU as a field that is included when users perform a search.

To accomplish this we need to modify the search query and tell WordPress to also look at the _sku post meta field. Inspired by “Search WordPress by Custom Fields without a Plugin” by Adam Balée, I have prepared the code for you below. You can add this code to your website using the Code Snippet plugin, or your own preferred method.

Note: If you search for a specific SKU that doesn’t match anything else, WooCommerce will automatically redirect to that specific product page. This is true for any search that returns only a single product.

If you’re using our WooCommerce Show Single Variations plugin, this will also enable searching for their SKU codes.

18 Shares

26 Comments

  1. stacy says:

    Hi James,

    Thank you for the informative posted. I want to be able to search for the product without having to add the dash (-) or the spacing. For example, my product sku 25-467 GCA and i want to be able to just type in the search box: 25467GCA and the product appears. Any help would be appreciated.

    Stacy

  2. Wesley van Wijngaarden says:

    Hi,
    Great script. But in backend when searching for a product gives one SKU code way too much results. 1 unique code give at least 19 results with all the same code.

    • James Kemp says:

      Hey Wesley, can you try changing the first if statement in each function to: if( is_admin() || ! is_search() || ! is_woocommerce() ) {

      It shouldn’t really run on the backend at all!

      • Wesley van Wijngaarden says:

        Hi James,
        Thx for your quick reply. And most important thx for the solution. Works well now.

  3. Humberto Albarrán says:

    My friend, your post is the solution I was looking for. But, I dont get where this code is going to be written.
    Do I have to edit woocommerce-core-functions.php? Thanks in advance.

  4. Punkie says:

    Thanks for your great code snippet! I will include it in all my woo projects now. You rock!

  5. Dave Schroeder says:

    This seems to break the Recently Viewed Products widget on search pages. It only shows the last product shown for all of them. Anyone have a fix for this?

  6. Mansoor Firoozeh says:

    Hi Dear James, tnx for sharing , How we can use it in the back-end? for example we want to search a meta_value (on the meta_key) of a product in the “Woocommerce Orders page” at the back-end.
    Please help me to search through a meta_key to check the meta_vaule.
    woocommerce function to search is : “woocommerce_shop_order_search_fields”

    please help me to do this.
    so much appreciated.
    Mansoor

  7. Abhishek Agrawal says:

    Hey James! nice piece of code.

    Can you pls suggest how to resolve the Recently Viewed section wherein last product is repeating several times after adding the snippet. Thanks much 🙂

  8. Fei says:

    Hi,
    I copied the code you provided in a code snippet but after activated it, it doesn’t produce the wanted results..
    Can you check my website?
    https://amavisse-wholesale….

    PS: I’m using wordpress 4.9 and woocommerce 3.2.5

    • James Kemp says:

      Hey Fei,

      I’ve just tested again locally and it works as expected. It could be your version of Woo. Is there an SKU I can use to test the search on your site?

  9. Sarah says:

    Hi James!

    I’ve tried adding the code but it doesn’t seem to be working. Could you check our sites to see if it’s working for you??

    http://www.allthingsa2z.com or http://www.vikingwholesalex.com

  10. Björn says:

    James, you really made my day. Big thank you for sharing your code.
    After searching for hours and being guided to the wrong directions all the time (SEO or use of plugin) I was about to give up. Then I had the idea to lookup SKU search and found your blog. Originally I wanted to improve my Woocommerce search to be able to lookup meta tags, but wasn’t able to find anything working.
    After testing your code I can confirm it is working and doing its job just excellent!
    If I want to add meta tag search now, am I right I need to add a hook to line 44? Would you mind guiding me on that one, because neither I do know the exact hook, nor am I sure how to edit your snippet the correct way.
    Highly appreciated, best regards,

    -Björn

  11. Damir says:

    Hi James,

    Code works great, thank you.
    I have 3 questions:

    1. how to add another meta key to the query, to search another meta besides SKU
    2. Then is it possible to add that functionality to backend, SKU search already works out of box but need other meta
    3. When I use woocommerce search widget it takes me directly to product page not the search results page

    Thank you very much.

    • James Kemp says:

      Hey,

      1. Just add another one of these OR (iconic_post_meta.meta_key = '_sku' AND iconic_post_meta.meta_value LIKE $1) after the first one.
      2. Yep, although it’s out of scope for this tutorial!
      3. It does this when only one product is found for the search.

      Hope this helps!

      • Björn says:

        Hey James,

        I tried this before with ‘tagged_as / .tagged_as / _tagged_as / tag / _tag’, but none of them seem to work.
        Am looking forward to add this search to shop search only, not admin frontend.
        Would be great if you could help.

        Thank you very much and best regards,

        -Björn

  12. dummy says:

    Hi Sir,

    I want to product search by multiple sku in frontend please help me to solve this ASAP.

  13. Jan says:

    Hey James,
    many thanks for posting this.
    Just a short question: I understand this works with the standard product search widget. As I’m trying to get this working for the standard general wordpress search I had to remove the ! is_woocommerce statement to show me the results. Then it works for SKUs (1 result). Just a normal text search gives me way too many results again. Any idea?
    Have a nice day and keep it up!

Leave a Reply

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