Introduction
WooCommerce is an open-source plugin for WordPress that converts a WordPress site into an eCommerce store. WooCommerce is simple, easy-to-install, easy-to-use, and highly customizable. You can create plugins for WooCommerce to extend its functionality without the need to modify core WordPress or WooCommerce. In this tutorial, we will cover the steps to take to build a simple WooCommerce plugin from scratch.
WooCommerce plugin development extends WordPress plugin development. Its fundamentals are similar to those of regular WordPress plugins and should also adhere to all WordPress plugin code standards. In order to customize WooCommerce, you will create a WordPress plugin that interacts with WooCommerce and extends its functionality.
Why consider WooCommerce Plugin Development?
If your WooCommerce store is very niche and unique and you want to personalize some of its features, you may need to create a custom plugin. The primary objective of a WooCommerce plugin is to create a personalized experience on the eCommerce platform that anyone can use, from WooCommerce newbies to experienced store owners. Additionally, WooCommerce plugins boost the core features of a WooCommerce store, thus allowing merchants to really “own” their store. You can create a WooCommerce plugin to solve a variety of problems that may not be available in core WooCommerce. For instance, you can enable a specific payment gateway, customize how to display products on the storefront, or add custom fields to specific products.
WooCommerce Plugin Development: Considerations
Before you embark on a journey to create a WooCommerce extension, ensure that you have explored the functionality of existing WooCommerce plugins in the WooCommerce extension store. If you do not find a plugin that solves your problem, you should consider WooCommerce plugin development.
To create a custom WooCommerce plugin from scratch, some WordPress development skills are necessary. A WooCommerce plugin developer should have an understanding of the technologies that power WordPress: HTML, CSS and JavaScript for the front end, and PHP, SQL for the back end. Here are the considerations for a WordPress developer.
Let us delve into the steps to create a WooCommerce plugin.
WooCommerce Plugin Development: Key Steps
In this section, we go through the broad steps in the process of creating a WooCommerce plugin. You will notice that a number of these steps are common to the steps in WordPress plugin development.
In this example, we’ll create a simple discounts plugin for WooCommerce that would enable you to create dynamic discounts based on the user. This is a simple plugin that applies a global discount to all products for a user.
Steps 1-3: Common with WordPress Plugin Development
In the process of WooCommerce plugin development, the first three steps are common to the steps in WordPress plugin development. They are:
- Step 1: Define the Requirements: The first step is to clearly define your development needs. When you know precisely about the problem you would like to solve, you are able to efficiently create a plugin.
- Step 2: Create a WordPress Plugin Directory Structure: This step guides you in creating an appropriate directory to store your plugin files. In short, we recommend that you create a single PHP file to contain the code of the plugin (/wp-content/plugins/my-plugin/my-plugin.php).
- Step 3: Configure your Plugin: In this step, you create a file header that contains the metadata of the plugin. Adding the header makes the plugin appear in the list of your WordPress plugins.
Step 4: Check if WooCommerce is Active
When creating a WooCommerce plugin, you would ideally only want the plugin to execute when WooCommerce is active on your WordPress site. To ensure that, you should wrap your PHP class around the following snippet.
Additionally, we’ll use the class WC_Dynamic_Discounts
with an empty constructor as a placeholder to complete the plugin further.
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// Put your plugin code here
if ( ! class_exists('WC_Dynamic_Discounts')){
class WC_Dynamic_Discounts{
public function __construct(){
}
}
}
}
This ensures that your code is executed only when WooCommerce is active and the class WC_Dynamic_Discounts
does not exist.
Step 5: Add an action to Capture Discount
In the next step, we will register actions to show and save a discount that is associated with every user. The action show_discount
will display the discount associated with every user, whereas save_discount
will save the custom profile value.
public function __construct(){
add_action('edit_user_profile', array( $this, 'show_discount'), 10, 1);
add_action( 'edit_user_profile_update', array( $this, 'save_discount'), 10, 1 );
}
Next, we need to define the functions show_discount and save_discount. We use get_user_meta() to get the details of the logged-in user.
public function show_discount($user) {
?>
<table>
<tr>
<th>Discount</th>
<td>
<input type="number" id="show_discount" name="show_discount" min="0" max="100" value="<?php echo get_user_meta($user->ID, 'show_user_discount', true) ?>">
</td>
<?php
if ( ! current_user_can( 'edit_user') ) {
<td>
<input type="submit" id="submit" name="submit">
</td>
?>
</tr>
</table>
<?php
}
public function save_discount($user) {
if ( ! current_user_can( 'edit_user') ) {
return false;
}
if(isset($_POST['show_discount'])) {
$show_user_discount = $_POST['show_discount'];
update_user_meta($user, 'show_discount', $show_user_discount);
}
}
In the plugin, we display the discount to each user. However, only if the user can edit their own details, we allow them to change the value of the discount.
Step 6: Apply Discount to Products
To display prices, you need to use the woocommerce_product_get_price
filter. You need to add it to the constructor and then define the function.
public function __construct(){
...
add_filter( 'woocommerce_product_get_price', array( $this, 'show_dynamic_price' ), 10, 2);
}
The definition of the function show_dynamic_price
gets the discount value of the current user and calculates the dynamic price.
//Add within the class body
public function show_dynamic_price($price, $product) {
$current_user_id = get_current_user_id();
$discount = floatval(get_user_meta($current_user_id,'show_discount', true));
if(!empty($discount)) {
// Discount is a value between 0 to 100 in percentage
$dynamic_price = $price - (($price * $discount)/100);
return $dynamic_price;
}
}
WooCommerce Plugin Development: Best Practices
WooCommerce extensions should adhere to WordPress plugin coding standards
Since WooCommerce plugin development is closely aligned with WordPress plugin development, all their best practices would apply to this case too. Here are a few other best practices for WooCommerce extensions.
Avoid custom database tables
As far as possible, avoid creating custom database tables. Instead, try and utilize WordPress post types, options, and taxonomies.
Declare Required and Supported WooCommerce Versions
You can declare the required and supported WooCommerce versions by adding the following lines in the plugin metadata.
WC requires at least: 4.0
WC tested up to: 4.1
Use a Woo Plugin Header
If you plan to sell your plugin on WooCommerce.com, you will need to add a Woo line in the plugin header comment, which checks for updates of the plugin on WooCommerce.com.
Woo: 12345:342928dfsfhsf8429842374wdf4234sfd
WooCommerce extensions should have a single objective
Their main objective is to create a pleasant experience on the merchant’s site – therefore, they should be straightforward and have a single core purpose that enhances WooCommerce features as much as possible. They shouldn’t have an objective that is not intended to enhance WooCommerce (ex: inserting spam links or linking to services outside of the WooCommerce ecosystem is unacceptable)
Do not override WooCommerce connections in Core
WooCommerce extensions can’t override WooCommerce connections in the core. For instance, you cannot create a branded top-level menu item or introduce a different system of data collection; these actions are forbidden.
Conclusion
In this step-by-step guide to WooCommerce plugin development, we explored how you can create a WooCommerce plugin from scratch.
As we saw, a custom WooCommerce plugin should be well thought, well developed, and tested. Though WooCommerce plugin development may seem incredibly complicated, it can be achieved with skills and a good understanding of WordPress.
For small and medium-sized organizations, the lack of in-house expertise, budget, and time may prove to be challenging in creating a WooCommerce plugin from scratch. If after careful consideration you decide that you don’t want to delve into the development of the plugin yourself, you can always use a reliable hiring partner like Codeable and find an expert WooCommerce developer who will do it for you.
WooCommerce plugin development and customization are some of the most popular services that our vetted experts offer on Codeable.