How to build a WooCommerce plugin: architecture and update-safe code
A guide to building a WooCommerce plugin that survives updates: structure, hooks, using WooCommerce APIs instead of direct database access, compatibility declarations and testing.
The Gihonix team · · 4 min read
Key takeaways
- Use hooks and WooCommerce APIs instead of editing core files or querying tables directly.
- Declare compatibility with WooCommerce features and test against new releases on staging.
- Keep one clear entry file, separate classes and escape or sanitise everything.
- Plan versioning, uninstall behaviour and a changelog from the start.
To build a WooCommerce plugin that survives updates, structure it as a normal WordPress plugin, load it only when WooCommerce is active, extend behaviour through hooks and WooCommerce's own data methods, declare which WooCommerce features you support and test on a staging site before each release. Update-safe code relies on public, documented extension points, never on internal details.
This guide describes the approach at a conceptual level. For exact APIs, always read the official WooCommerce developer documentation and the WordPress Plugin Handbook.
What should a plugin's structure look like?
Keep it simple and predictable.
- One main file with the plugin header, which does little more than load the rest.
- Separate classes for settings, admin screens, front-end output and integrations.
- A clear folder layout for templates, assets and languages.
- A single prefix on functions, classes, options and hooks to avoid collisions.
Avoid putting everything in one long file. Small classes are easier to test and change.
How do you make sure WooCommerce is active?
Your plugin should check that WooCommerce is available before it runs any WooCommerce-specific code, and show a notice in the admin if it is not. Hook your setup to a point in the loading order that guarantees plugins are loaded, so you do not break the site when WooCommerce is deactivated.
Why use hooks and not edit templates or core?
Hooks, meaning actions and filters, are the official way to add or change behaviour. Code that overwrites core files or copies large templates is likely to break when WooCommerce updates.
- Actions let you run code at a point, such as after an order is created.
- Filters let you change a value, such as a price or a message.
- If you need to alter output, prefer a hook. If you must override a template, copy the minimum and keep it under version control so you can compare it after updates.
How should you read and write store data?
Use WooCommerce's data methods, such as the order, product and customer objects, instead of reading database tables directly. WooCommerce has changed how order data is stored, with an optional custom order tables feature known as High-Performance Order Storage. Code that queries post tables for orders may fail when a store enables that feature.
Good habits:
- Get orders and products through WooCommerce functions and objects.
- Save custom data with the object's meta methods.
- Avoid hard-coded table names for WooCommerce data.
How do you declare compatibility?
WooCommerce lets plugins declare whether they support certain features, such as custom order tables or the block-based cart and checkout. Declare only what you have tested. Merchants then see accurate compatibility information, and you avoid breaking stores that enable those features.
How do you keep the plugin secure?
Follow standard WordPress practice:
- Sanitise input from forms, requests and settings.
- Escape output in templates and admin pages.
- Check capabilities so only authorised users can change settings.
- Use nonces to protect actions from forged requests.
- Prepare database queries if you must write your own.
The WordPress security guidance explains each of these.
How do you handle settings, updates and removal?
- Store settings with the WordPress options API, under your own prefix.
- Version your plugin and store the installed version, so you can run an upgrade routine when data structures change.
- Decide what happens on uninstall: remove your data, or ask the owner.
- Keep a changelog so store owners know what each release does.
How do you test it?
Test on a staging copy that mirrors production.
- A fresh install, and an upgrade from the previous version.
- With and without the newest WooCommerce release.
- With common plugins that also touch checkout or orders.
- Different user roles, and mobile checkout.
- With debugging enabled, to catch warnings.
For wider testing practice, see QA before launch. If you are deciding whether to build at all, read when a plugin beats three plugins.
Work with Gihonix
Gihonix is a software, AI and web development company based in Coimbatore, India. Our custom plugin development team writes WooCommerce plugins with documented, update-safe code for clients in Coimbatore, across India and worldwide. Contact us to scope yours.
Frequently asked questions
Do I need to know PHP to build a WooCommerce plugin?
Yes. WooCommerce plugins are written in PHP using WordPress and WooCommerce APIs, along with JavaScript for some admin and block features.
Should I edit the WooCommerce core files?
No. Core edits are overwritten at the next update. Use hooks and your own plugin instead.
What is HPOS?
High-Performance Order Storage is a WooCommerce feature that stores order data in dedicated tables. Plugins should use WooCommerce's order methods so they work with it.
How often should I test the plugin?
Test before each release and whenever WooCommerce, WordPress or a related plugin ships a major update.