Kestrel — WooCommerce experts — WordPress tutorials

Safely add custom code to WooCommerce stores

One of the most beautiful features of WooCommerce and WordPress is the ability to customize your online store to do just about anything you can dream of. 

You can add custom code to WooCommerce to change its behavior. You can make sure your online store behaves exactly as you want it to, from the messages you show to customers to the way shop pages look to all sorts of advanced functionality (whether you use WooCommerce extensions or hire a developer for your own custom code).

This is in contrast to a hosted software app, where you have no access to the underlying code of the app to customize it – what you see is what you get. 

While you likely want to work with a developer to make significant changes to your store, there are lots of small code changes that can beak tweak or improve your online storefront – you’ll often find blog posts with a suggested code snippet to enhance your store (including WooCommerce tutorials on this blog!). 

Let’s walk through our recommended way to add custom code to WooCommerce so you can make your own small enhancements or modifications to your online store.

Add custom code to WooCommerce

You’ll often see recommendations to add custom code to WooCommerce via your theme’s functions.php file. This is the “old” way to add code snippets to your site, which I do not recommend. 

Themes modify the appearance of your site, and oftentimes you don’t want these modifications flying away if you change the way your site looks! A much better way to get your wings wrapped around site changes is to use a snippet manager – you can add multiple code snippets, and choose whether to activate or deactivate a code snippet (similar to a plugin).

First, we’ll install the Code Snippets plugin. Go to Plugins > Add New Plugin and search for “code snippets” – you should find the Code Snippets plugin as the first result. Click Install Now and Activate to add the plugin to your site.

This adds a new Snippets menu to your WP admin. You can go to Snippets > Add New to add a new custom code snippet. I’ll use an example snippet I often add to my test sites to make all orders editable (not just those that are unpaid):

When you add the snippet, you can name it with a title, enter the code for the snippet, and optionally give the snippet a description or tags. I find the description helpful to remind me what the code is doing at a glance when I view snippets on my site.

While the default option is to Run snippet everywhere, you can also choose to run the snippet only in certain places – for example, if you want to only change the site frontend (for snippets that affect your storefront, like adding a tracking script), or to “run once”, which is helpful for code you only want to run “on demand”.

Once you’re done writing a brief description, you can save your snippet and activate it! This starts to run the code on your site and make the modifications from your code snippet. You can then activate and deactivate the snippet just like a plugin.

Almost any time you add custom code to WooCommerce, you’ll want to use a PHP (the default) snippet; there are some cool things you can do with content or other snippet types, but pretty much any code you’d find in a tutorial will assume you’re using PHP to add custom code to WooCommerce.

Need some inspiration? Check out our WooCommerce tutorials for a great starting point to customizing your store.

Ian Misner Avatar

About the author…

Join the Flock

Product updates, sales & coupons, Woo tutorials. Zero spam.


Comments

Leave a Reply

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