Five WooCommerce Snippets We Use On Every Project
Last Updated January 21st, 2022 · WordPress
WooCommerce is, by far, the most popular e-commerce platform used by our clients. At Link Software, we are regularly asked to implement WooCommerce both on existing sites and new ones we build from scratch. The big selling feature of WooCommerce is how easy it is to modify and extend. You can do this using any of the hundreds of plugins that other developers have created. Or, if you have a little bit of coding knowledge, you can adjust how your online store functions with some easy-to-implement WooCommerce snippets.
Let’s take a look at five of the most common WooCommerce snippets we use on our clients’ sites. We’re going to discuss how to remove optional data from your product pages. Figure out how to make adjustments to your customer’s account screens. And we’ll show you the best way to adjust how products get listed in shopping carts. First, before any of that, we’ll walk you through the safest and best way to implement these on your own site.
How To Safely Use WooCommerce Snippets on Your Site
A WooCommerce snippet is simply a piece of code you add to your site. This can be done in a variety of ways. You can make your own custom WordPress plugin to implement the snippet. The most common way to implement some custom code is to add it to your theme’s functions.php file. We find a lot of clients do this themselves or hire a cheap freelancer to do it for them, but it is a risky approach. The main problem with this approach is that your changes will eventually be lost when the theme receives an update. So how can you make changes to a theme without losing your work someday?
The first step before adding any code to your WordPress site is to make a child theme. This basic rule applies not only to implementing WooCommerce snippets but any WordPress code. We have written a very length guide on how to safely and properly create a WordPress child theme. We strongly recommend you start there before making any changes to your WordPress site’s code or structure.
Once your WordPress child theme is created you are ready to change the functions.php. Here are the five most common WooCommerce snippets we implement for our clients. There are literally hundreds of others but we find these to be pretty common requests.
WooCommerce Snippet to Remove the WooCommerce SKU from Product Pages
Most WooCommerce themes that you can find for free, or purchase online, include a tiny SKU code on each product page. Of course, not every online retailer uses an SKU for their products or cares to make them known to the public. Thankfully, you can remove the SKU label from your product listings with a tiny piece of code. Add these few lines of code to your child theme’s functions.php file:
add_filter( 'wc_product_sku_enabled', 'custom_remove_sku', 10 );
function custom_remove_sku( $return, $product ) {
if ( !is_admin() && is_product() ) {
return false;
} else {
return true;
}
}
That tiny snippet of WooCommerce code will automatically remove the SKU badge from each of your product pages. If you ever decide you want to bring the SKU label back you simply remove that chunk of code from your child theme.
Change Your WooCommerce “In Stock” Message
WooCommerce comes with some basic inventory controls right out of the box. You can tell WooCommerce to manage stock levels on an individual product basis through the admin. One of the neat features of this is that, on the front end of your site, customers will know if your items are in stock or not. Of course, this message isn’t always something people want their visitors to see. Or, in some cases, they want to be able to adjust the exact wording of the message. Thankfully, this can all be controlled by the following WooCommerce snippet:
add_filter( 'woocommerce_get_stock_html', 'custom_remove_stock_messages', 10, 2 );
function custom_remove_stock_messages( $html, $product ) {
if ( $product->is_in_stock() ) {
$html = 'INSERT YOUR STOCK MESSAGE HERE';
}
return $html;
}
This one is pretty simple. Just replace the INSERT YOUR STOCK MESSAGE HERE
text in the snippet above with whatever you want to display on your site. This snippet serves double duty as well. You can use it to remove the stock message entirely. Just remove the INSERT YOUR STOCK MESSAGE HERE
text. It’s as simple as that.
Disable Coupon Codes on Your WooCommerce Store
We use this particular WooCommerce snippet quite often. A lot of our clients either don’t offer coupon codes or do so sparingly. In those situations, showing a coupon code field on your site leads to confusion. Many customers won’t order unless they can find a code and, when they can’t, ask the site owners for one directly. In general, giving a customer an option to enter in a discount code when none are available generates unnecessary confusion and hassle. Drop this handful of lines in your child theme’s functions.php to remove coupon codes entirely from your cart template:
add_filter('woocommerce_coupons_enabled', 'custom_remove_coupon_cart', 10);
function custom_remove_coupon_cart($enabled) {
if (is_cart()) {
$enabled = false;
}
return $enabled;
}
Use this WooCommerce snippet to disable coupon fields on your actual checkout form:
add_action('init', 'custom_remove_coupon_checkout');
function custom_remove_coupon_checkout() {
remove_action('woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10);
}
Those two chunks of code should be all you need to hide coupon codes from your WooCommerce site. Again, this is a pretty common request from our clients. It’s often the case when they are using free or premium themes. Most custom theme projects will have this kind of functionality either excluded already or handled through options or settings.
Hide Breadcrumb Navigation on WooCommerce Sites
For some reason, text-based breadcrumb navigation is a common design choice with free WooCommerce themes. Our working theory is that you see these so much because the WooCommerce codebase generates them automatically. That makes them easy to implement for theme designers. You can remove these pretty quickly with the following WooCommerce code snippet:
add_action( 'init', 'custom_remove_breadcrumbs' );
function custom_remove_breadcrumbs() {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
}
Here’s a slight warning: breadcrumb navigation does have some use for improving your site’s accessibility. Most of the time they are unwanted navigation choices for store owners. But, if you are concerned about accessibility or have certain requirements you need, consider keeping the breadcrumb navigation around.
Remove Product Links from Your Shopping Cart Template
One of the first rules of good e-commerce design is to limit the ways your visitors can exit the checkout process. By doing so, you give your potential customers fewer ways to click away from their ongoing checkout process. With that in mind, you can easily remove all of the individual product links on your shopping cart template with this WooCommerce snippet:
add_filter( 'woocommerce_cart_item_permalink', 'custom_remove_cart_product_link', 10 );
function custom_remove_cart_product_link() {
return __return_null();
}
Marketing experts will recommend making drastic design changes to your shopping cart and checkout templates. The most common practice is to totally remove, or dramatically modify, all menus and navigation by removing choices. You can handle that with a child theme and some heavier lifting than we have time to cover here. But, as a first place to start, using this particular piece of code is a good idea.
Manage WooCommerce Snippets without Code
We were doing similar changes for clients so often that we decided to build a premium plugin. It can be pretty intimidating to implement WooCommerce snippets on your own. Making a child theme can be tough and adding and modifying code is usually more than most online store owners want to deal with.
Our plugin, Remove WooCommerce Features, handles all of the issues we discussed in this plugin plus dozens and dozens of others. You can purchase, install, and start modifying your WooCommerce store in minutes with the plugin.
If you have other WooCommerce problems that you need dealing with please contact us today for help. Link Software has years of custom WordPress development experience.