How to hide the SKU in WooCommerce with a function

Posted on 27 February 2023

Are you looking for web hosting?

Start today from just £2.99/month

Our super-fast web hosting comes with the Direct Admin control panel, super-fast SSD drives, NGINX-Apache config for top performance, brotli compression, 99.9% uptime, daily backups and 1Gbps connectivity

Sign Up

Introduction

If you’re running an online store using WooCommerce, you might want to hide the SKU from the frontend. The SKU, or Stock Keeping Unit, is a unique identifier assigned to each product in your inventory. By default, WooCommerce displays the SKU on the product page, which can be useful for internal inventory management but might not be necessary or desirable for customers to see. Fortunately, it’s easy to hide the SKU from the frontend in WooCommerce using a simple function.

Why Hide the SKU?

Before we dive into the code, let’s take a moment to consider why you might want to hide the SKU in the first place. There are a few reasons you might want to do this:

  • Clutter
  • Confidentiality
  • Aesthetics

Hiding the SKU in WooCommerce

The easiest way to hide the SKU in WooCommerce is by adding a simple function to your theme’s functions.php file. Here’s the code you’ll need:

add_filter( 'wc_product_sku_enabled', '__return_false' );

This code hooks into the wc_product_sku_enabled filter and returns false, which tells WooCommerce not to display the SKU on the frontend. You can add this code to your theme’s functions.php file using a code editor or by using a plugin such as Code Snippets. Once you’ve added the code and saved the changes, the SKU will no longer be displayed on the frontend of your store.

It’s worth noting that this code will hide the SKU for all products in your store. If you only want to hide the SKU for certain products, you’ll need to modify the code to add some conditional logic. For example, you could use the get_the_ID() function to get the ID of the current product and then check if it matches a list of product IDs you want to hide the SKU for.

Are you looking for web hosting?

Start today from just £2.99/month

Our super-fast web hosting comes with the Direct Admin control panel, super-fast SSD drives, NGINX-Apache config for top performance, brotli compression, 99.9% uptime, daily backups and 1Gbps connectivity

Sign Up

Hiding the SKU for Specific Products

Here’s an example of how to modify the code to hide the SKU for specific products:

function hide_sku_for_specific_products( $enabled, $product ) { // Replace these with the IDs of the products you want to hide the SKU for $hidden_products = array( 123, 456, 789 ); if ( in_array( $product->get_id(), $hidden_products ) ) { $enabled = false; } return $enabled; } add_filter( 'wc_product_sku_enabled', 'hide_sku_for_specific_products', 10, 2 );

In this modified code, we’ve created a new function called hide_sku_for_specific_products() that takes two parameters: $enabled, which is the default value of the SKU display setting, and $product, which is the current product being displayed. We’ve also created an array called $hidden_products that contains the IDs of the products we want to hide the SKU for.

Inside the function, we use the in_array() function to check if the current product’s ID is in the $hidden_products array. If it is, we set $enabled to false, which hides the SKU. Finally, we return the modified value of $enabled.

We’ve also modified the add_filter() function to use our new hide_sku_for_specific_products() function and added two additional parameters: 10, which is the priority of the filter (the lower the number, the earlier the filter is applied), and 2, which is the number of arguments the function accepts (in this case, $enabled and $product).

With this modified code, the SKU will be hidden for the products whose IDs are listed in the $hidden_products array, but displayed for all other products.

Conclusion

Hiding the SKU from the frontend in WooCommerce is a simple process that can help improve the aesthetics and functionality of your online store. By using the wc_product_sku_enabled filter and adding some conditional logic, you can easily hide the SKU for all products or specific products in your inventory. This can help improve the user experience for your customers and make your online store more professional and streamlined.

Share this post with your friends, followers and connections!


Subscribe to our mailing list

* indicates required

View previous campaigns.