A complete tutorial on Custom WooCommerce Plugin Development. We’ll develop a WooCommerce plugin with step by step code explanation. On completion of this tutorial you will have a complete understanding of how to build a WooCommerce Plugin with admin and frontend stuff.
I wrote this because I have come across many WooCommerce Plugin Development tutorials. But I never found a single one that covers all the aspects of a good WC plugin. This tutorial is a complete guide for all those who are looking for how to do Custom WooCommerce Plugin Development.
Prerequisites
For this tutorial, you must have good PHP/MySQL and WordPress WooCommerce Development skills. If you are new to PHP or WordPress world this tutorial is not for you. I am not going to explain what PHP If Statement or Function is. You must be very familiar with WordPress and WooCommerce Hooks, CPT, etc My goal in this tutorial is to combine all the things and show to an intermediate level developer to gain necessary skills for WooCommerce plugin development.
Let’s Get Started
WooCommerce plugins development is the same as regular WordPress plugins. A plugin is a piece of PHP code that adds new features or extends functionality to your WordPress websites.
Directory Structure
Organization is key in order to make any type of code work efficiently. Each developer have its own plugin directory structure, here in this tutorial I’ll use common practices.
Below are the directories and files required for Manufacturer plugin
- wcik-manufacturer Directory: Plugin root directory
- Add below to wcik-manufacturer directory
- include Directory
- templates Directory
- manufacturer.php File
- Add below to wcik-manufacturer/include directory
- product-detail.php File
- admin Directory
- Add below to wcik-manufacturer/include/admin directory
- post_type.php File
- meta_box.php File
- add_column.php File
- Add below to wcik-manufacturer/templates directory
- manufacturer Directory
- Add below to wcik-manufacturer/templates/manufacturer directory
- archive-manufacturer.php File
- single-manufacturer.php File
Plugin File Header
The main PHP file should include header comment what tells WordPress that a file is a plugin and provides information about the plugin. File Headers are placed one per line on top of the plugin file. Header values are filtered before they are used but it must not contain HTML code Elements or HTML Tags.
Open manufacturer.php and add the below header code in it.
The WooCommerce Manufacturer plugin is ready to install. Go to WordPress admin plugins page and check Manufacturer plugin within plugins listing as shown in below image. Click Activate as like normal WP plugin.
No Direct Access to Plugin
After plugin header add the following code to manufacturer.php file
It prevent public user to directly access your .php files through URL. Because if your file contains some I/O operations it can eventually be triggered (by an attacker) and this might cause unexpected behavior.
So, Using the snippets can prevent access from your files (directly) and ensures that your plugin files will be executed within the WordPress environment only.
Check if WooCommerce is active
The first thing I recommend is to check WC is active. if WooCommerce isn’t activated, the functionality is simply ignored instead of producing fatal errors.
Include Directory Files
The below code adds 4 files already created within include directory. require_once() function will include only once and will ignore the same file if included again. plugin_dir_path() is a WordPress function it gets the plugin directory path with _FILE__ passed in.
Your manufacturer.php file should look like below one
7 Things You Should Never Do With Your WooCommerce Store